Introducing Angular Injection Services

Source: Internet
Author: User

In fact, angular injection service is quite complex, at present, see the source code also only understand half, in order not to mislead everyone, I do not dare to speak too complex, afraid of their own understanding is wrong.

First of all we need to know three ways to inject angular:

The first type: Inference

var function ($scope) {            }

The second type: annotation

var function ($location) {            console.log (' Module: Code injection $location Success ');            Console.log ($location);         = [' $location ']; $injector. Invoke (MyModule);

The third type: inline

var MyTest = [' $location ',function($location) {}]
 varMyTest = [' $location ',function($location) {Console.log (' Test: Code injection $location Success ');        Console.log ($location); }]        varMyModule =function($location) {Console.log (' Module: Code injection $location Success ');        Console.log ($location); } angular.module (' MyApp ', []). Run (function($injector) {console.log ($injector); //functionality is the same as injection-time declarations, but when injected, it is sometimes possible to inject a solution within a function by declaring a recurring dependency problem. Console.log ($injector. Get (' $http ')); //Dependency Injection used to get an objectConsole.log ($injector. Annotate (MyTest)); //injection function, when you define a function, beyond the specifications of NG, you can choose to inject the function in this way to achieve the purpose of accessing NG built-in services$injector. Invoke (MyTest); //whether the current service existsConsole.log ($injector. has (' Myfact ')); //mymodule. $inject = [' $location ']                //$injector. Invoke (MyModule);}). Factory (' Myfact ',function(){                return{}}). controller (' Myctrl ',function(){                            })

Introducing Angular Injection Services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.