最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular minification with directive controller? - Stack Overflow

programmeradmin1浏览0评论

If I have the following:

myapp.directive('directivename', ...

    return {
        ...
        restrict: 'E',
        controller: MyController,
        ...
    }

    function MyController($scope, $somethingelse) {
        // Contents of controller here
    }
);

How do I modify this such that MyController will not get destroyed when minified? I am getting the following error:

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

If I have the following:

myapp.directive('directivename', ...

    return {
        ...
        restrict: 'E',
        controller: MyController,
        ...
    }

    function MyController($scope, $somethingelse) {
        // Contents of controller here
    }
);

How do I modify this such that MyController will not get destroyed when minified? I am getting the following error:

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

Share Improve this question edited Jan 3, 2015 at 18:57 PSL 124k21 gold badges256 silver badges243 bronze badges asked Dec 31, 2014 at 22:49 RolandoRolando 62.6k103 gold badges278 silver badges422 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 21

It can be resolved by using explicit dependency annotation. What you have it implicit annotation which causes issues while minification. You could use $inject or inline array annotation to annotate the dependencies in the directive as well.

MyController.$inject = ['$scope', '$somethingelse'];

function MyController($scope, $somethingelse) {
    // Contents of controller here
}

Or in the directive:

return {
    ...
    restrict: 'E',
    controller: ['$scope', '$somethingelse', MyController],
    ...
}

Or register your controller using .controller syntax

app.controller('MyController', ['$scope', '$somethingelse', MyController]);

and set up controller name in the directive instead of the constructor.

return {
    ...
    restrict: 'E',
    controller: 'MyController',
    ...
}

You can also take a look at ng-annotate with which you don't need to use explicit annotation.

Usually, the following approach is used:

myapp.controller('MyController', ['$scope', '$somethingelse', function($scope, $somethingelse) {
  ...
}]);

to avoid such problems.


You can use like this:

return {
    restrict: 'EA',
    template: ...,
    scope: {},
    controller: ["$scope","$rootScope", function ($scope,$rootScope) {
       //code here
    }],
    link: function (scope) {
    }
}
发布评论

评论列表(0)

  1. 暂无评论