Understanding Angular Services
2013-10-16 16:54
483 查看
Understanding Angular Services
Angular services are singletons objects or functions
that carry out specific tasks common to web apps. Angular has a number of built in services, such as the
for making requests to a server. Like other core Angular variables and identifiers, the built-in services always start with
as
To use an Angular service, you identify it as a dependency for the component
(controller, service, filter or directive) that depends on the service. Angular's dependency injection subsystem takes care of the rest. The Angular injector subsystem is in charge of service instantiation, resolution
of dependencies, and provision of dependencies to components as requested.
Angular injects dependencies using "constructor" injection. The dependency is passed to the component's
factory/constructor function. Because JavaScript is a dynamically typed language, Angular's dependency injection subsystem cannot use static types to identify service dependencies. For this reason a component must, explicitly, define its dependencies by using
one of the injection annotation methods. For example, by providing a
Or by providing an "inline" injection annotation:
Application developers are free to define their own services by registering their name, and service factory function, in Angular modules.
The purpose of the service factory function is to generate the single object, or function, that represents the service to the rest of the application. That object, or function, will then be injected into any component (controller, service,
filter or directive) that specifies a dependency on the service.
Angular factory functions are executed lazily. That is, they are only executed when needed to satisfy a dependency, and are then executed exactly once for each service. Everything that is dependent on this service gets a reference to the single instance generated
by the service factory.
About Angular Dependency Injection
Creating Angular Services
Managing Service Dependencies
Testing Angular Services
Angular Service API
What are Angular Services?
Angular services are singletons objects or functionsthat carry out specific tasks common to web apps. Angular has a number of built in services, such as the
$http service, which provides access to the browser's
XMLHttpRequestobject
for making requests to a server. Like other core Angular variables and identifiers, the built-in services always start with
$(such
as
$httpmentioned above). You can also create your own custom services.
Using a Service
To use an Angular service, you identify it as a dependency for the component(controller, service, filter or directive) that depends on the service. Angular's dependency injection subsystem takes care of the rest. The Angular injector subsystem is in charge of service instantiation, resolution
of dependencies, and provision of dependencies to components as requested.
Angular injects dependencies using "constructor" injection. The dependency is passed to the component's
factory/constructor function. Because JavaScript is a dynamically typed language, Angular's dependency injection subsystem cannot use static types to identify service dependencies. For this reason a component must, explicitly, define its dependencies by using
one of the injection annotation methods. For example, by providing a
$injectproperty:
var MyController = function($location) { ... }; MyController.$inject = ['$location']; myModule.controller('MyController', MyController);
Or by providing an "inline" injection annotation:
var myService = function($http) { ... }; myModule.factory('myService', ['$http', myService]);
Defining a Service
Application developers are free to define their own services by registering their name, and service factory function, in Angular modules.The purpose of the service factory function is to generate the single object, or function, that represents the service to the rest of the application. That object, or function, will then be injected into any component (controller, service,
filter or directive) that specifies a dependency on the service.
Angular factory functions are executed lazily. That is, they are only executed when needed to satisfy a dependency, and are then executed exactly once for each service. Everything that is dependent on this service gets a reference to the single instance generated
by the service factory.
Related Topics
About Angular Dependency InjectionCreating Angular Services
Managing Service Dependencies
Testing Angular Services
Related API
Angular Service APIInjector API
相关文章推荐
- 现在就开始使用AngularJS的三个重要原因
- angularjs使用lhgdialog做对话框控件
- Duplicates in a repeater are not allowed in AngularJS
- angular todo
- Triangular Sums
- 使用AngularJS构建大型Web应用
- 使用AngularJS构建大型Web应用
- Promise in AngularJS
- Promise in AngularJS
- angular style, class
- angular controller
- angular directive =,&
- angular
- angular 写的一个分页功能
- AngularJS框架速写
- angular 和 backbone 区别
- AngularJS 基础
- AngularJS 中文资料+工具+库+Demo 大搜集
- 解决jinja2 模板和AngularJS分隔符冲突的方法
- 浅析AngularJS