how to use factory service in my code please help me this for data export excel. if you have any other logic or example for table convert to excel please share with me i need this. but at this time i need show to run this code I Want to run .factory('Excel',function($window) this factory*
Error: [$injector:strictdi] function($window) is not using explicit annotation and cannot be invoked in strict mode
//MODULE
export default angular.module('goApp.main', [ngRoute])
.config(routing).factory('Excel',function($window){
var uri='data:application/vnd.ms-excel;base64,',
template='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64=function(s){return $window.btoa(unescape(encodeURIComponent(s)));},
format=function(s,c){return s.replace(/{(\w+)}/g,function(m,p){return c[p];})};
return {
tableToExcel:function(tableId,worksheetName){
var table=$(tableId),
ctx={worksheet:worksheetName,table:table.html()},
href=uri+base64(format(template,ctx));
return href;
}
};
})
.component('main', {
template: require('./main.html'),
controller: MainController,
controllerAs: 'Ctrlmain'
})
.name;
//CONSTROLLER
import angular from 'angular';
const ngRoute = require('angular-route');
import routing from './main.routes';
export class MainController {
/*@ngInject*/
constructor($http, Excel,$timeout,$window,$scope,socket, $location, Auth,NgMap,$uibModal) {
this.$http = $http;
this.socket = socket;
$scope.exportToExcel=function(tableId){ // ex: '#my-table'
var exportHref=Excel.tableToExcel(tableId,'sheet name');
$timeout(function(){location.href=exportHref;},100); // trigger download
}
}
}
Use inline array annotation for the factory:
̶e̶x̶p̶o̶r̶t̶ ̶d̶e̶f̶a̶u̶l̶t̶ ̶a̶n̶g̶u̶l̶a̶r̶.̶m̶o̶d̶u̶l̶e̶(̶'̶g̶o̶A̶p̶p̶.̶m̶a̶i̶n̶'̶,̶ ̶[̶n̶g̶R̶o̶u̶t̶e̶]̶)̶
export default angular.module('goApp.main', ['ngRoute'])
̶.̶c̶o̶n̶f̶i̶g̶(̶r̶o̶u̶t̶i̶n̶g̶)̶.̶f̶a̶c̶t̶o̶r̶y̶(̶'̶E̶x̶c̶e̶l̶'̶,̶f̶u̶n̶c̶t̶i̶o̶n̶(̶$̶w̶i̶n̶d̶o̶w̶)̶{̶
.config(routing).factory('Excel',['$window', function($window){
var uri='data:application/vnd.ms-excel;base64,',
template='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64=function(s){return $window.btoa(unescape(encodeURIComponent(s)));},
format=function(s,c){return s.replace(/{(\w+)}/g,function(m,p){return c[p];})};
return {
tableToExcel:function(tableId,worksheetName){
var table=$(tableId),
ctx={worksheet:worksheetName,table:table.html()},
href=uri+base64(format(template,ctx));
return href;
}
};
̶}̶)̶
}])
For more information, see