错误:[$ injector:strictdi]函数($ window)-需要显式注释

时间:2018-07-19 14:41:21

标签: angularjs angularjs-directive angularjs-scope

如何在我的代码angularjs中运行工厂

如何在我的代码中使用工厂服务,请帮助我进行数据导出。 如果您有将表转换为excel的任何其他逻辑或示例,请与我分享我需要的信息。但是这时我需要显示运行此代码 我要在此工厂运行.factory('Excel',function($ window)**

  

错误:[$ injector:strictdi]函数($ window)没有使用显式注释,因此无法在严格模式下调用

// 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
        }
        }
}

1 个答案:

答案 0 :(得分:0)

为工厂使用内联数组批注:

̶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;
      }
    };
  ̶}̶)̶
  }])

有关更多信息,请参见