angularjs - controllers using as normal function or array notation -
what difference between these 2:
angular.module('myapp' ,[]) .controller('mycontroller', function($scope){...});
and
angular.module('myapp' ,[]) .controller('mycontroller, ['$scope', function($scope){...})];
this quite complicated new angularjs me. syntax different java , c.
many thanks.
there's nothing difference between them. both code works same way. if use first code , when minify code confuse.
look example:
.controller('mycontroller', function(a){...});//$scope changed
and code won't work angularjs code uses $scope variable doesn't take first, second, third, , on parameters.
so, second code safer first if when minify code, still takes same variable i.e. $scope.
look example:
.controller('mycontroller', ['$scope', function(a){...})];//a refers $scope
so, above code works fine when minify code $scope injected in place of a. so, if pass multiple parameters ordering matters in example. @ following:
.controller('mycontroller', ['$scope','$timeout', function(s,t){...})];
s injected $scope , t injected $timeout. if change order of them ['$timeout','$scope', function(s,t){...})]
s $timeout , t $scope. so, ordering matters in example in first example code ordering won't matter name matters $scope, $timeout.
there's way inject variables if use first example code below:
mycontroller.$inject = ['$scope'];
for multiple parameters,
mycontroller.$inject = ['$scope','$timeout'];
so, there 3 kinds of annotation:
- implicit annotation - first example code
- $inject property annotation - $inject method
- inline array annotation - second example code
Comments
Post a Comment