Answer the question
In order to leave comments, you need to log in
What is the difference between writing myApp.controller('ctrl', ['$scope', function ($scope) {}]) and myApp.controller('ctrl', function ($scope) {})?
The whole point is in the title)
I don’t quite understand why the first option is used everywhere if the second one is shorter!?
Answer the question
In order to leave comments, you need to log in
Because Angular injects dependencies by argument names. And some minifiers and compilers change the argument names. In the end, everything breaks down. The first option is used to explicitly set the list of dependency names to inject.
In general, in a normal way, either this option is used:
angular.module('app').controller('FooController', FooController);
FooController.$inject = ['$service1', '$service2'];
function FooController($service1, $service2) {}
angular.module('app').controller('FooController', FooController);
/*@ngInject*/
function FooController($service1, $service2) {}
And you write <html ng-app="app" ng-strict-di>
and understand everything.
Read the Implicit Annotation section .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question