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