D
D
Dmitry2016-02-03 18:09:48
JavaScript
Dmitry, 2016-02-03 18:09:48

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

2 answer(s)
B
bromzh, 2016-02-03
@pyHammer

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) {}

Either they take it , set up their collector (there are a lot of plugins) and insert a specific comment where necessary:
angular.module('app').controller('FooController', FooController);

/*@ngInject*/
function FooController($service1, $service2) {}

M
Mikhail Osher, 2016-02-03
@miraage

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 question

Ask a Question

731 491 924 answers to any question