Angularjs缩小最佳实践 [英] Angularjs minify best practice
问题描述
我读<一个href=\"http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html\">http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html和
事实证明,如果你压缩JavaScript angularjs依赖注入有问题
所以我不知道如果不是
I'm reading http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html and it turned out that angularjs dependency injection has problems if you minify your javascript so I'm wondering if instead of
var MyController = function($scope, $http) {
$http.get('https://api.github.com/repos/angular/angular.js/commits')
.success(function(commits) {
$scope.commits = commits
})
}
你应该使用
var MyController = ['$scope', '$http', function($scope, $http) {
$http.get('https://api.github.com/repos/angular/angular.js/commits')
.success(function(commits) {
$scope.commits = commits
})
}]
所有的一切我认为第二个片段是旧版本angularjs,但是....
all in all I thought the second snippet was for the old version of angularjs but ....
我应该总是使用注射方式(第二个)?
Should I always use the inject way (the second one) ?
推荐答案
是,永远幸福!即使你minifer转换$范围变量a $ HTTP到变量b所以这样一来,他们的身份仍然是preserved 的字符串。
Yes, always! So this way even if your minifer converts $scope to variable a and $http to variable b, their identity is still preserved in the strings.
请参阅 AngularJS文档的页面,向下滚动到的上微小的注
See this page of AngularJS docs, scroll down to A Note on Minification.
更新
另外,你可以使用 NG-注释 NPM包在构建过程中避免这种详细程度。
Alternatively, you can use ng-annotate npm package in your build process to avoid this verbosity.
这篇关于Angularjs缩小最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!