0

我正在尝试使用角度组件创建动态 templateUrls。但我得到这个错误:

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

angular
    .module('myApp')
    .component('fieldComponent', {
        templateUrl: function ($element, $attrs) {
           return $attrs.templateUrl || 'some/path/default.html'
        },
        controller:fieldComponentController,
        controllerAs:'vm',
        $routeConfig: [
            {path: '/dashboard', component: 'dashboardApp', name: 'Dashboard', useAsDefault: true}
        ]
    });

随着文件的缩小,它的抛出错误如上所述。那么我在哪里以及如何注入这个依赖项?

4

2 回答 2

1

我猜你在 fieldComponentController 中的 DI 被缩小破坏了。当代码被缩小时,您的依赖项的名称将更改为“e”之类的东西,Angular 不知道如何处理。

解决此问题的一种方法是利用 ng-annotate ( https://github.com/olov/ng-annotate ) 以缩小安全的方式重写这些变量名称。

如果你使用像 Webpack 这样的构建工具来进行打包和压缩,你可以简单地将 ngAnnotatePlugin 添加到你的配置中,并在你的控制器定义的顶部包含字符串 'ngInject'

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout)        
{
"ngInject";
...
});

ES2015 版本:

export class MyCtrl {
    constructor($scope, $timeout){
        'ngInject';
    }
}
于 2016-09-13T14:05:49.000 回答
0

按照@Matt Richards 的建议,我可以通过在顶部添加 / @ngInject / 来做到这一点。

/*@ngInject*/
angular
    .module('myApp')
    .component('fieldComponent', {
        templateUrl: function ($element, $attrs) {
           return $attrs.templateUrl || 'some/path/default.html'
        },
        controller:fieldComponentController,
        controllerAs:'vm',
        $routeConfig: [
            {path: '/dashboard', component: 'dashboardApp', name: 'Dashboard', useAsDefault: true}
        ]
    });

但是,最后我最终ng-include宁愿采用动态模板方法,并节省了我覆盖单元测试用例的时间。

于 2016-09-13T17:23:41.897 回答