angular.js/docs/content/guide/dev_guide.services.understanding_services.ngdoc

37 lines
1.7 KiB
Text
Raw Normal View History

2011-06-06 15:50:35 +00:00
@ngdoc overview
@name Developer Guide: Angular Services: Understanding Angular Services
@description
Angular services are singletons that carry out specific tasks common to web apps, such as the
{@link api/ng.$http $http service} that provides low level access to the browser's
2011-06-06 15:50:35 +00:00
`XMLHttpRequest` object.
2012-09-26 13:30:55 +00:00
To use an Angular service, you identify it as a dependency for the dependent (a controller, or
2011-06-06 15:50:35 +00:00
another service) that depends on the service. Angular's dependency injection subsystem takes care
2012-09-26 13:30:55 +00:00
of the rest. The Angular injector subsystem is in charge of service instantiation, resolution of
2011-06-06 15:50:35 +00:00
dependencies, and provision of dependencies to factory functions as requested.
Angular injects dependencies using "constructor" injection (the service is passed in via a factory
2012-01-16 07:28:10 +00:00
function). Because JavaScript is a dynamically typed language, Angular's dependency injection
2011-06-06 15:50:35 +00:00
subsystem cannot use static types to identify service dependencies. For this reason a dependent
must explicitly define its dependencies by using the `$inject` property. For example:
myController.$inject = ['$location'];
2012-09-26 13:30:55 +00:00
The Angular web framework provides a set of services for common operations. Like other core Angular
2012-01-16 07:28:10 +00:00
variables and identifiers, the built-in services always start with `$` (such as `$http` mentioned
2011-06-06 15:50:35 +00:00
above). You can also create your own custom services.
## Related Topics
2012-02-27 20:14:48 +00:00
* {@link di About Angular Dependency Injection}
2011-06-06 15:50:35 +00:00
* {@link dev_guide.services.creating_services Creating Angular Services}
* {@link dev_guide.services.managing_dependencies Managing Service Dependencies}
* {@link dev_guide.services.testing_services Testing Angular Services}
## Related API
* {@link api/ng Angular Service API}
2011-06-06 15:50:35 +00:00
* {@link api/angular.injector Injector API}