What does ngInject do in the following piece of code?

'ngInject'; does nothing by itself, it’s just a string literal. A tool called ng-annotate uses it as a flag : if a function starts with 'ngInject';, it will be processed by ng-annotate.

Basically, ng-annotate will transform

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

to

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

in order to make the code minification-safe.

If you’re not using ng-annotate, you can safely ignore or remove the expression. Be careful though, you might break the build process of the project if it does use ng-annotate. For more information about ng-annotate and what it does, see https://github.com/olov/ng-annotate

Leave a Comment