forked from angular/code.angularjs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_guide.services.creating_services.html
58 lines (50 loc) · 3.23 KB
/
dev_guide.services.creating_services.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<h1>Developer Guide: Angular Services: Creating Angular Services</h1>
<div class="developer-guide-angular-services-creating-angular-services"><fieldset class="workInProgress"><legend>Work in Progress</legend>
This page is currently being revised. It might be incomplete or contain inaccuracies.</fieldset>
<p>While angular offers several useful services, for any nontrivial application you'll find it useful
to write your own custom services. To do this you begin by registering a service factory function
that angular's DI will use to create the service object when it is needed.</p>
<p>The <code>angular.service</code> method accepts three parameters:</p>
<ul>
<li><code>{string} name</code> - Name of the service.</li>
<li><code>{function()} factory</code> - Factory function (called just once by DI).</li>
<li><code>{Object} config</code> - Configuration object with the following properties:
<ul><li><code>$inject</code> - {Array.<string>} - Array of service ids this service depends on. These services
will be passed as arguments into the factory function in the same order specified in the <code>$inject</code>
array. Defaults to <code>[]</code>.</li>
<li><code>$eager</code> - {boolean} - If true, the service factory will be called and the service will be
instantiated when angular boots. If false, the service will be lazily instantiated when it is first
requested during instantiation of a dependant. Defaults to <code>false</code>.</li></ul></li>
</ul>
<p>The <code>this</code> of the factory function is bound to the root scope of the angular application.</p>
<p>All angular services participate in <a href="#!/guide/dev_guide.di">dependency injection (DI)</a> by registering
themselves with angular's DI system (injector) under a <code>name</code> (id) as well as by declaring
dependencies which need to be provided for the factory function of the registered service. The
ability to swap dependencies for mocks/stubs/dummies in tests allows for services to be highly
testable.</p>
<p>Following is an example of a very simple service. This service depends on the <code>$window</code> service
(which is passed as a parameter to the factory function) and is just a function. The service simply
stores all notifications; after the third one, the service displays all of the notifications by
window alert.</p><div ng:non-bindable><pre class="brush: js;">
angular.service('notify', function(win) {
var msgs = [];
return function(msg) {
msgs.push(msg);
if (msgs.length == 3) {
win.alert(msgs.join("\n"));
msgs = [];
}
};
}, {$inject: ['$window']});
</pre></div><h3>Related Topics</h3>
<ul>
<li><a href="#!/guide/dev_guide.services.understanding_services">Understanding Angular Services</a></li>
<li><a href="#!/guide/dev_guide.services.registering_services">Registering Angular Services</a></li>
<li><a href="#!/guide/dev_guide.services.managing_dependencies">Managing Service Dependencies</a></li>
<li><a href="#!/guide/dev_guide.services.injecting_controllers">Injecting Services Into Controllers</a></li>
<li><a href="#!/guide/dev_guide.services.testing_services">Testing Angular Services</a></li>
</ul>
<h3>Related API</h3>
<ul>
<li><a href="#!/api/angular.service"><code>Angular Service API</code></a></li>
</ul></div>