Angular Dependency Injection

Source: Internet
Author: User

Angular uses modular organization and relies on injected design, which reduces the coupling between modules and makes the modules easier to reuse. It also supports declarative programming styles.

In angular, a module usually corresponds to a JS file, which contains config,controller,service,filter,directive.

Angular.module (' myApp ', [' Ngroute ', ' Ngresource '])    . config (function ($httpProvider) {        $ httpprovider.defaults.headers.post[' Acception/json ';    })    . Controller (' Myctrl ', function ($scope, $http) {        $scope. user = [' Alice ', ' Bob '];    })    . directive (' Custom ', function () {        return {templeteurl: ' tpl/custom.html '};    })    . Filter (' Count ', function () {       return function (content) {          return content + "one";       }    })

Where MyApp is the module name, Ngroute and Ngresource are dependent modules

$httpProvider is the configuration Provider,myctrl is the controller, $http is dependent on the service

Custom is a customized directive

Count is a filter

Dependency Injection  

In angular, controller,directive,filter,service are given as a factory method, and the parameter name of the factory method corresponds to the service that the factory method relies on.

Such as:

App.controller (' Myctrl ', function ($scope, $http) {   //...})

Before the above function executes, Angular injector will generate a $scope instance and $http instance, and pass in the method, if you want to handle the JS compression, then the parameter name may change. Angular Injector will not properly inject the dependent service. Thus, there is another way of writing:

App.controller (' Myctrl ', [' $scope ', ' $http ', function ($scope, $http) {   //...}])

Declares a dependent number as an array of strings, because string constants are not compressed.

  

Angular Dependency Injection

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.