【问题标题】:Error: [$injector:strictdi] function($window) - Explicit annotation required错误:[$injector:strictdi] 函数($window) - 需要显式注释
【发布时间】:2018-07-19 14:41:21
【问题描述】:

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

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

错误:[$injector:strictdi] 函数($window)没有使用显式注解,不能在严格模式下调用

//模块

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;

//控制器

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

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope


    【解决方案1】:

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

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

    有关详细信息,请参阅

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多