1

When I define angular like following, it will cause issue on using minification this javascript, which is Error: $injector:modulerr Module Error.

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. Is there a way I can avoid not writing code as the second case?

3 Answers 3

2

You can use $inject:

function MyController($scope) {
}

MyController.$inject = ['$scope'];
angular.module('myModule').controller('MyController', MyController);
Sign up to request clarification or add additional context in comments.

Comments

1

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). It is a tool that processes your javascript code and helps you avoid repeating yourself. 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.

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.

Comments

0

You have this for Grunt : https://www.npmjs.com/package/grunt-ng-annotate I'm pretty sure you could find the same thing for webpack/gulp etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.