Byc
Byc

Reputation: 183

Difference in how angular controller accepts $scope

I'm completely new to angular. I see two ways of passing $scope to an angular controller:

app.controller("myCtrl", function($scope) {
...
});

app.controller('myCtrl', ['$scope', function($scope) {
...
}]);

what is the difference between these two?

Upvotes: 2

Views: 40

Answers (1)

4rlekin
4rlekin

Reputation: 758

All "in-angular" code is obviously evaluated client side - meaning when you write a module that includes something (say other module), actual including happens during executing javascript in the browser.

So when you pass $scope into function, angular evaluate it to be actual $scope object, because it recognizes it as its way to indicate said object.

But if you are using some kind of minifier for your .js files, among other things that happens is minification of variable names, and since from purely JS point of view it looks like you are passing variable named '$scope' into anonymous function it would be minified to something shorter.

And here is the problem - angular needs to see that it is a $scope, but after minification it can as well be named 'a' or something like that... BOOM whole perfect plan ruined. :'(

The problem was solved by allowing passing a list in place of simple function (second notation in your question) and making angular understand that each subsequent element of the list is string containing "angularish" name of each subsequent (and by now minified) argument to function which should be last element of said list.

And obviously explicit strings cannot be simply minified without losing their meaning - so they don't :)
Problem solved !
Long live angularjs

Upvotes: 1

Related Questions