【问题标题】:Error after Minification of angular js. Error: [$injector:unpr] Unknown provider: eProvider <- e <- makeErrorsDirective角度 js 缩小后出错。错误:[$injector:unpr] 未知提供者:eProvider <- e <- makeErrorsDirective
【发布时间】:2016-05-09 16:33:08
【问题描述】:

我使用 Gulp 来缩小我的整个 js 文件。缩小后,我收到如下错误:

[$injector:unpr] Unknown provider: eProvider <- e <- makeErrorsDirective.

我的控制器文件中有一个自定义指令。

var myhubdashboardControllers = angular.module('vpdashboardmodule', []);

.directive('mhDashboard', function ($http, authService, apiService) {
    return {
        restrict: 'EA',
        scope: {
            name: '@',
            dash: '@',
            report: '@',
            disname: '@',
            disdesc: '@',
            distot: '@'
        },
        templateUrl: 'views/dashboard/dashboard-direc.html',
        link: function (scope, element, attr) {
            scope.linkChk = scope.name;
            switch (scope.linkChk) {
                case 'Shipped This Week':
                    scope.url = 'erp/JobShipmentList/PostCpsVwShipmentCount';
                    scope.shipstatus = "Departure";
                    scope.period = "ThisWeek";
                    scope.basicfilter = "Open";
                    scope.linkName = "Shipments";
                    scope.linkDesc = "Shipped This Week";
                    break;

}) };

这是我的应用程序中使用的代码。

【问题讨论】:

标签: javascript angularjs minify bundling-and-minification gulp-minify


【解决方案1】:

Angular 并不总是适用于缩小。

如果你作为例子写这个:

angular.controller("MyCtrl", function ($scope) {...});

然后$scope 将在缩小期间更改为无意义的东西。

如果改为:

angular.controller("MyCtrl", ["$scope", function (s) {...}]);

那么调用函数中的第一个参数(这里是s)并不重要,只要字符串是"$scope"

有关详细信息,请参阅文档中的 https://docs.angularjs.org/tutorial/step_05#a-note-on-minification

如果您需要更多帮助,您必须发布有问题的代码,而不仅仅是错误。

【讨论】:

    【解决方案2】:

    您必须在字符串数组中注入服务和控制器是有原因的。

    如果你想给控制器注入作用域,你必须使用

    angular.module('yourApp')
        .controller('yourController',['$scope',function($scope){
        }]);
    

    缩小会改变变量名,如果你在注入服务或控制器时不使用那个字符串数组,它会像

     angular.module('yourApp')
        .controller('yourController',function(e){
        });
    

    因此,angular 将无法理解 'e' 代表什么,因此会出现错误。 永远记住顺序也很重要。

    .directive('mhDashboard', ['$http','authService','apiService', function ($http, authService, apiService) {
        return {
            restrict: 'EA',
            scope: {
                name: '@',
                dash: '@',
                report: '@',
                disname: '@',
                disdesc: '@',
                distot: '@'
            },
            templateUrl: 'views/dashboard/dashboard-direc.html',
            link: function (scope, element, attr) {
                scope.linkChk = scope.name;
                switch (scope.linkChk) {
                    case 'Shipped This Week':
                        scope.url = 'erp/JobShipmentList/PostCpsVwShipmentCount';
                        scope.shipstatus = "Departure";
                        scope.period = "ThisWeek";
                        scope.basicfilter = "Open";
                        scope.linkName = "Shipments";
                        scope.linkDesc = "Shipped This Week";
                        break;
    }
    }])
    

    【讨论】:

    • 我的控制器文件中有一个自定义指令。缩小后它会产生类似 [$injector:unpr] Unknown provider: eProvider
    • 感谢拉古。我为控制器做了,但我错过了指令。现在可以了。
    • 有同样的问题,我不得不在我的服务和指令以及我的控制器上使用["$http", function ($http) { ... }] 语法。感谢您指出这一点。 +1 了
    【解决方案3】:

    我遇到了同样的问题,即使我使用 gulp-ng-annotate,但它只发生在 $stateProvider 和 ngDialog 解析:

    $stateProvider
      .state('orders', {
        url: '/orders',
        templateUrl: 'templates/orders.html',
        controller: 'OrdersController as vm',
        resolve: {
          authenticate: function (Auth) {
            return Auth.getAuthResolve();
          }
        }
      });
    

    Resolve需要这样写:

        resolve: {
          authenticate: ['Auth', function (Auth) {
            return Auth.getAuthResolve();
          }]
        }
    

    所以感觉 ng-annotate 并没有将数组注入到解析中,而只是注入到控制器/服务/工厂构造函数中。

    【讨论】:

    • 你应该在任何有注入的地方使用 ng-anotate,即使在解析内部(在每个函数之前)
    【解决方案4】:

    我在使用 angular-ui-router-title 时遇到了问题。改变后

    $titleProvider.documentTitle(function($rootScope) {
        return $rootScope.$title + ' my title';
    });
    

    $titleProvider.documentTitle(['$rootScope', function($rootScope) {
        return $rootScope.$title + ' my title';
    }]);
    

    错误不再出现。

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 2016-01-11
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多