AngularJS Dependency Injection Format -
I'm new to angular, I do not understand why $ scope
and $ Http
function
such as
app.controller ('PlayerController', ['$ scope', '$ http', function ($ radius) , $ Http) {var audio = document.createElement ('audio'); $ scope.audio = audio; // ...
Why is this? Reading:
They are placed so that when you reduce it the code is not broken. You can only
function ($ scope, $ http) {// use $ scope}
and You will know by the parameter names, which you will have to give the $ scope
and $ http
service. But when you reduce the code, something like this might be < / P>
function (a, b) {// use a}
where there is a $ field
and b is $ Http
service. For this reason, AngularJS allows you to specify the string name of the services that you need to inject to modify the javascript minifaming string Does not mean, therefore
['$ scope', '$ http', function ($ radius, $ http) {}]
['$ scope', '$ http', function (a, b) {// a $ scope / b is $ http}]
< Hr> You should read more about it, there are several ways to inject. The following are equivalent:
// The first method is the app controller ('player controller', ['$ radius', '$ http', function ($ radius, $ http) {}]); // second way app.controller ('PlayerController', function ($ radius, $ http) {// if you do not reduce your javascript then this will work}]); // Third way function player controller ($ amount, $ http) {} player controller ['$ injection'] = ['radius', '$ http']; App.controller ('Player controller', player controller);
This applies to the same thing (Dependency Injection Style) services, factories etc. It is not specific to controllers.
Comments
Post a Comment