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.
AngularJS Example The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. The myCtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object.
AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is defined using ng-controller directive. A controller is a JavaScript object that contains attributes/properties, and functions.
ng-controller directive tells AngularJS what controller to use with this view. AngularJS application mainly relies on controllers to control the flow of data in the application.
The ng-controller directive defines the application controller. In AngularJS, a controller is defined by a Javascript construction function, which is used in AngularJS scope and also the function $scope) is defined when the controller is defining and it returns the concatenation of the $scope. firstname and $scope.
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.
There is not much difference between the two approaches. Both code works same way. But if you use the second code then it will confuse you after you minify your code.
Look for an example:-
app.controller("MyCtrl", function(a){ ... });//$scope is changed to a
And your code won't work as AngularJs code uses $scope variable as it doesn't take first, second, third, and so on parameters.
So, the first code is safer than second as if when you minify the code, it will still takes same variable i.e. $scope.
Look for an example:
app.controller("MyCtrl", ['$scope', function(a){...}]);//a refers to $scope
So, the above code works fine when you minify the code as $scope is injected in place of a.So, if you pass multiple parameters then ordering matters in this example.
Look at the following:
app.controller("MyCtrl", ['$scope','$timeout' ,function(a,t){...}]);
where a is injected as $scope and t is injected as $timeout.
So if you change the orders of parameters passed as
app.controller("MyCtrl", ['$timeout','$scope', function(a,t){...}]); where a is $timeout and t is $scope.
So, ordering matters in this example but in your second example code ordering won't matter as name matters like $scope, $timeout.
There's also another way to inject variables if you use your first example code like below:
MyCtrl.$inject = ['$scope'];
For multiple parameters,
MyCtrl.$inject = ['$scope','$timeout'];
So, there are mainly three kinds of annotation:
You can learn more about it here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With