简体   繁体   English

Angular javascript minification导致错误:$ injector:modulerr模块错误

[英]Angular javascript minification cause Error: $injector:modulerr Module Error

When I define angular like following, it will cause issue on using minification this javascript, which is Error: $injector:modulerr Module Error. 当我定义像下面的角度时,它将导致使用缩小此javascript的问题,这是错误:$ injector:modulerr模块错误。

angular.module('myModule').controller('MyController', function ($scope) {

});

Now, if I write my angular as following, it'll be fine after minification. 现在,如果我按照下面的方式写出我的角度,那么在缩小之后它会很好。

angular.module('myModule').controller('MyController', ["$scope", function ($scope) {

}]);

During the minification, the first way will convert the $scope to other variable name, looks like that the issue. 在缩小期间,第一种方式将$ scope转换为其他变量名称,看起来就是问题。 Is there a way I can avoid not writing code as the second case? 有没有办法避免不编写代码作为第二种情况?

You can use $inject : 你可以使用$inject

function MyController($scope) {
}

MyController.$inject = ['$scope'];
angular.module('myModule').controller('MyController', MyController);

If you have some kind of build-process (or you want to introduce a pretty small one), have a look at the ng-annotate project ( https://github.com/olov/ng-annotate ). 如果您有某种构建过程(或者您想引入一个非常小的构建过程),请查看ng-annotate项目( https://github.com/olov/ng-annotate )。 It is a tool that processes your javascript code and helps you avoid repeating yourself. 它是一个处理您的JavaScript代码的工具,可以帮助您避免重复自己。 What it does is basically reading the first version of your code and generating an $inject property for you as shown in the answer of Pedro Nascimento. 它的作用基本上是阅读代码的第一个版本并为你生成$ inject属性,如Pedro Nascimento的答案所示。

There are several plugins for grunt, gulp, webpack and so on, but it's also pretty easy to just use this one module if you don't have a build process yet. 有几个插件用于grunt,gulp,webpack等等,但是如果你还没有构建过程,那么使用这个模块也很容易。

你有这个Grunt: https ://www.npmjs.com/package/grunt-ng-annotate我很确定你可以找到webpack / gulp等相同的东西......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM