Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  9] [ 4]  / answers: 1 / hits: 103856  / 11 Years ago, fri, september 13, 2013, 12:00:00

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')
.then(function(response) {
$scope.commits = response.data
})
}

you should use


var MyController = ['$scope', '$http', function($scope, $http) {
$http.get('https://api.github.com/repos/angular/angular.js/commits')
.then(function(response) {
$scope.commits = response.data
})
}]

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) ?


More From » angularjs

 Answers
16

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.



See this page of AngularJS docs, scroll down to A Note on Minification.



UPDATE



Alternatively, you can use ng-annotate npm package in your build process to avoid this verbosity.


[#75711] Thursday, September 12, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
acaciac

Total Points: 317
Total Questions: 117
Total Answers: 128

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
;