@@ -97,9 +97,8 @@ created by this recipe.
97
97
Note: All services in Angular are singletons. That means that the injector uses each recipe at most
98
98
once to create the object. The injector then caches the reference for all future needs.
99
99
100
- Since Factory is more a powerful version of the Value recipe, you can construct the same service with it.
101
- Using our previous `clientId` Value recipe example, we can rewrite it as a Factory recipe like
102
- this:
100
+ Since a Factory is a more powerful version of the Value recipe, the same service can be constructed with it.
101
+ Using our previous `clientId` Value recipe example, we can rewrite it as a Factory recipe like this:
103
102
104
103
```javascript
105
104
myApp.factory('clientId', function clientIdFactory() {
@@ -134,11 +133,11 @@ token.
134
133
135
134
<div class="alert alert-success">
136
135
**Best Practice:** name the factory functions as `<serviceId>Factory`
137
- (e.g. apiTokenFactory). While this naming convention is not required, it helps when navigating the code base
136
+ (e.g., apiTokenFactory). While this naming convention is not required, it helps when navigating the codebase
138
137
or looking at stack traces in the debugger.
139
138
</div>
140
139
141
- Just like with Value recipe, Factory recipe can create a service of any type, whether it be a
140
+ Just like with the Value recipe, the Factory recipe can create a service of any type, whether it be a
142
141
primitive, object literal, function, or even an instance of a custom type.
143
142
144
143
@@ -153,7 +152,7 @@ function UnicornLauncher(apiToken) {
153
152
154
153
this.launchedCount = 0;
155
154
this.launch = function() {
156
- // make a request to the remote api and include the apiToken
155
+ // Make a request to the remote API and include the apiToken
157
156
...
158
157
this.launchedCount++;
159
158
}
@@ -170,7 +169,7 @@ myApp.factory('unicornLauncher', ["apiToken", function(apiToken) {
170
169
```
171
170
172
171
173
- This is, however, exactly the use-case that Service recipe is the most suitable for.
172
+ This is, however, exactly the use-case that the Service recipe is the most suitable for.
174
173
175
174
The Service recipe produces a service just like the Value or Factory recipes, but it does so by
176
175
*invoking a constructor with the `new` operator*. The constructor can take zero or more arguments,
@@ -189,7 +188,7 @@ myApp.service('unicornLauncher', ["apiToken", UnicornLauncher]);
189
188
Much simpler!
190
189
191
190
Note: Yes, we have called one of our service recipes 'Service'. We regret this and know that we'll
192
- be somehow punished for our mis-deed . It's like we named one of our offspring 'Child'. Boy,
191
+ be somehow punished for our misdeed . It's like we named one of our offspring 'Child'. Boy,
193
192
that would mess with the teachers.
194
193
195
194
@@ -199,8 +198,8 @@ As already mentioned in the intro, the Provider recipe is the core recipe type a
199
198
all the other recipe types are just syntactic sugar on top of it. It is the most verbose recipe
200
199
with the most abilities, but for most services it's overkill.
201
200
202
- Provider recipe is syntactically defined as a custom type that implements a `$get` method. This
203
- method is a factory function just like the one we use in Factory recipe. In fact, if you define
201
+ The Provider recipe is syntactically defined as a custom type that implements a `$get` method. This
202
+ method is a factory function just like the one we use in the Factory recipe. In fact, if you define
204
203
a Factory recipe, an empty Provider type with the `$get` method set to your factory function is
205
204
automatically created under the hood.
206
205
@@ -248,7 +247,7 @@ and wires (injects) all provider instances only.
248
247
249
248
During application bootstrap, before Angular goes off creating all services, it configures and
250
249
instantiates all providers. We call this the configuration phase of the application life-cycle.
251
- During this phase services aren't accessible because they haven't been created yet.
250
+ During this phase, services aren't accessible because they haven't been created yet.
252
251
253
252
Once the configuration phase is over, interaction with providers is disallowed and the process of
254
253
creating services starts. We call this part of the application life-cycle the run phase.
@@ -259,9 +258,9 @@ creating services starts. We call this part of the application life-cycle the ru
259
258
We've just learned how Angular splits the life-cycle into configuration phase and run phase and how
260
259
you can provide configuration to your application via the config function. Since the config
261
260
function runs in the configuration phase when no services are available, it doesn't have access
262
- even to simple value objects created via Value recipe.
261
+ even to simple value objects created via the Value recipe.
263
262
264
- Since simple values, like url prefix , don't have dependencies or configuration, it is often handy
263
+ Since simple values, like URL prefixes , don't have dependencies or configuration, it's often handy
265
264
to make them available in both the configuration and run phases. This is what the Constant recipe
266
265
is for.
267
266
@@ -317,7 +316,7 @@ Let's take a look at how we would create a very simple component via the directi
317
316
on the `planetName` constant we've just defined and displays the planet name, in our case:
318
317
"Planet Name: Greasy Giant".
319
318
320
- Since the directives are registered via Factory recipe, we can use the same syntax as with factories.
319
+ Since the directives are registered via the Factory recipe, we can use the same syntax as with factories.
321
320
322
321
```javascript
323
322
myApp.directive('myPlanet', ['planetName', function myPlanetDirectiveFactory(planetName) {
@@ -340,7 +339,7 @@ We can then use the component like this:
340
339
</html>
341
340
```
342
341
343
- Using Factory recipes you can also define Angular's filters and animations, but the controllers
342
+ Using Factory recipes, you can also define Angular's filters and animations, but the controllers
344
343
are a bit special. You create a controller as a custom type that declares its dependencies as
345
344
arguments for its constructor function. This constructor is then registered with a module. Let's
346
345
take a look at the `DemoController`, created in one of the early examples:
@@ -351,7 +350,7 @@ myApp.controller('DemoController', ['clientId', function DemoController(clientId
351
350
}]);
352
351
```
353
352
354
- The DemoController is instantiated via its constructor every time the app needs an instance of
353
+ The DemoController is instantiated via its constructor, every time the app needs an instance of
355
354
DemoController (in our simple app it's just once). So unlike services, controllers are not
356
355
singletons. The constructor is called with all the requested services, in our case the `clientId`
357
356
service.
@@ -365,12 +364,12 @@ To wrap it up, let's summarize the most important points:
365
364
- There are five recipe types that define how to create objects: Value, Factory, Service, Provider
366
365
and Constant.
367
366
- Factory and Service are the most commonly used recipes. The only difference between them is that
368
- Service recipe works better for objects of custom type, while Factory can produce JavaScript
367
+ the Service recipe works better for objects of a custom type, while the Factory can produce JavaScript
369
368
primitives and functions.
370
369
- The Provider recipe is the core recipe type and all the other ones are just syntactic sugar on it.
371
370
- Provider is the most complex recipe type. You don't need it unless you are building a reusable
372
371
piece of code that needs global configuration.
373
- - All special purpose objects except for Controller are defined via Factory recipes.
372
+ - All special purpose objects except for the Controller are defined via Factory recipes.
374
373
375
374
<table class="table table-bordered code-table">
376
375
<thead>
0 commit comments