最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why we Inject our dependencies two times in angularjs? - Stack Overflow

programmeradmin4浏览0评论

I'm new in angular, want to know why and when we should inject all our needed dependencies two times.

Example :

var analysisApp=angular.module('analysisApp',[]);

analysisApp.controller('analysisController',function($scope,$http,$cookies,$state,globalService){   

});

But we can also write the above code as :

var analysisApp=angular.module('analysisApp',[]);

analysisApp.controller('analysisController',['$scope','$http','$cookies','$state','globalService',function($scope,$http,$cookies,$state,globalService){ 

}]);

Why ?

I'm new in angular, want to know why and when we should inject all our needed dependencies two times.

Example :

var analysisApp=angular.module('analysisApp',[]);

analysisApp.controller('analysisController',function($scope,$http,$cookies,$state,globalService){   

});

But we can also write the above code as :

var analysisApp=angular.module('analysisApp',[]);

analysisApp.controller('analysisController',['$scope','$http','$cookies','$state','globalService',function($scope,$http,$cookies,$state,globalService){ 

}]);

Why ?

Share Improve this question asked Sep 4, 2015 at 4:53 Hashir HussainHashir Hussain 6824 gold badges9 silver badges28 bronze badges 2
  • its dependency annotation see this one docs.angularjs/guide/di – Kalhan.Toress Commented Sep 4, 2015 at 4:58
  • 2 possible duplicate of Angularjs minify best practice – SomeKittens Commented Sep 4, 2015 at 5:24
Add a ment  | 

3 Answers 3

Reset to default 13

This is to make the app minsafe.

Careful: If you plan to minify your code, your dependency names will get renamed and break your app.

When you will(or may), minify all the files, the dependencies are replaced by the words like a, b, ... etc.

But, when you use array and string like syntax, as shown in the second snippet, the string are never minifies and can be used for mapping. So, the app knows that a is $scope(See below example).

Example:

// The minified version
var _ = angular.module('analysisApp', []);

_.controller('analysisController', ['$scope', '$http', '$cookies', '$state', 'globalService', function (a, b, c, d, e) {
    a.name = 'John Doe'; // Now a here is `$scope`.
}]);

See Angular Docs

Here is nice article for making your app minsafe with Grunt.

For minification best practices: Angularjs minify best practice

Dependency injection in Angular works by stringifying the function and detecting its arguments. This is quite fragile, especially when minifying your code, because these variable names will be mangled, and so your app will break. The workaround is to tell Angular the name of these injections with a string, which won't get mangled. In other words, if your code gets minified it would look like this:

analysisApp.controller('analysisController',
['$scope','$http','$cookies','$state','globalService', function(a,b,c,d,e) {

}]);

Under the covers, Angular will match $scope to a, $http to b, and so on.

Another possibility is to use ngannotate, which is a pre-processor tool to add to your build process. It will take the first code, and turn it into the second.

var analysisApp=angular.module('analysisApp',[]);

analysisApp.controller('analysisController',['$scope','$http','$cookies','$state','globalService',function($scope,$http,$cookies,$state,globalService){ 

}]);

In this case when u minify your code the above code may bee like

var analysisApp=angular.module('analysisApp',[]);

analysisApp.controller('analysisController',['$scope','$http','$cookies','$state','globalService',function(a,b,c,d, e){ 

}]);

In this case a, b, c, d will holde the refrence of '$scope','$http','$cookies','$state','globalService' accordingly and the things will work as expected.

'$scope','$http','$cookies','$state','globalService' will not be changed because these are string as string is not changed in minification

analysisApp.controller('analysisController',function($scope,$http,$cookies,$state,globalService){   

});

But in this case after minification it may bee like

analysisApp.controller('analysisController',function(a, b, c, d, e){   

});

Now all angular objects like $scope and other have lost their meaning And things will not work.

发布评论

评论列表(0)

  1. 暂无评论