Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ngInject do in the following piece of code?

Tags:

angularjs

AngularJS controller code:

function AuthConfig($stateProvider, $httpProvider) {   'ngInject';    // Define the routes   $stateProvider    .state('app.login', {     url: '/login',     templateUrl: 'auth/auth.html',     title: 'Sign in'   })    .state('app.register', {     url: '/register',     templateUrl: 'auth/auth.html',     title: 'Sign up'   });  };  export default AuthConfig; 

I am not able to figure out what is the use of ngInject. Could someone please help me?

like image 574
datavinci Avatar asked Oct 04 '17 04:10

datavinci


People also ask

What is ngInject?

'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.

What is the second argument in watch?

The $watch keep an eye on the variable and as the value of the variable changes the angular JS $what runs a function. This function takes two arguments one is the new value and another parameter is the old value.

What is $inject in AngularJS?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider.

Which of the following statement is true in the case of routeProvider?

Answer: D is the correct answer. 30) Which of the following statement is true in the case of $routeProvider? It is a service.


2 Answers

'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

like image 64
WirelessKiwi Avatar answered Oct 31 '22 21:10

WirelessKiwi


Usually ngInject is needed for the application to work when minified once you deploy in production. If you remove it and use optimize version it should fail.

Read more about AngularJs Minification and Annotation

like image 41
Sajeetharan Avatar answered Oct 31 '22 21:10

Sajeetharan