Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

javascript - Reason for using array notation when defining AngularJS Controller

Apologies if this question sounds too obvious.

I've recently starting exploring and learning AngularJS. I've gone through some good tutorials -

.. and there are a few other that I've seen.

I'm not saying that I've read/studied all the documents.

Question starts here -

Now, coming to the question, I see that the definition of a Controller is different in one place and it's different in some other -

One definition uses a sort of array notation (not sure of the official term) for injection:

app.controller("MyCtrl", ['$scope', function($scope){
    $scope.someData = "Array notation";
}]);

And there's this, with no array:

app.controller("MyCtrl", function($scope){
    $scope.someData = "non-array notation";
});

Not saying this is the only thing that I'm trying to understand but yes, I'd definitely love to understand the difference.

Is there a major difference between the two?

Thanks a lot.

Note: I did search for similar questions in SO but couldn't find what I was looking for. Sorry.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The difference is that when the second is minified, the parameter name will be minified and angular will no longer be able to inspect the arguments to figure out which dependencies to inject. The array syntax with the dependency in a string means that it is minification safe.

There is a library called ng-annotate which will change the second example into the first example so that the code is again minification safe.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...