Angular minification with directive controller?

元气小坏坏 提交于 2019-12-04 04:46:27
PSL

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) {
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!