` tags,
first without a value (yielding the default color) and then with an assigned color value.
- 下面的代码把指令`myHighlight`属性使用到两个`
`标签里,一个没有赋值,一个赋值了颜色。
+ 下面的代码把指令`myHighlight`特征使用到两个`
`标签里,一个没有赋值,一个赋值了颜色。
+
+makeExample('cb-dependency-injection/ts/app/app.component.html','highlight','app/app.component.html (highlight)')(format='.')
:marked
The following image shows the effect of mousing over the `` tag.
+
下图显示了鼠标移到``标签的效果:
+
figure.image-display
img(src="/resources/images/cookbooks/dependency-injection/highlight.png" alt="Highlighted bios")
:marked
@@ -561,12 +604,14 @@ figure.image-display
:marked
## Define dependencies with providers
## 使用供应商来定义依赖
+
In this section we learn to write providers that deliver dependent services.
在这个部分,我们学习如何编写供应商来提供依赖服务。
### Background
### 背景知识
+
We get a service from a dependency injector by giving it a ***token***.
我们从一个依赖注入器,通过给他一个***令牌(token)***来获取一个服务。
@@ -612,7 +657,7 @@ figure.image-display
We have to register our _own_ application providers manually,
usually in the `providers` array of the `Component` or `Directive` metadata:
- Angular初始化一些他自己建立和需要的注入器,附带一些供应商。我们必须要亲自手动注册属于_自己_的供应商,通常在`组件`或者`指令`的元数据里面的`供应商`数组(里面注册)。
+ Angular初始化一些他自己建立和需要的注入器,附带一些供应商。我们必须要亲自手动注册属于_自己_的供应商,通常在`组件`或者`指令`的元数据里面的`providers`数组(里面注册)。
+makeExample('cb-dependency-injection/ts/app/app.component.ts','providers','app/app.component.ts (providers)')
:marked
### Defining providers
@@ -622,7 +667,7 @@ figure.image-display
We mention the class in the `providers` array and we're done.
目前一个简单的类供应商是最典型的例子。
- 我们在`供应商`的数值里面提到该类就行了。
+ 我们在`providers`的数值里面提到该类就行了。
+makeExample('cb-dependency-injection/ts/app/hero-bios.component.ts','class-provider','app/hero-bios.component.ts (class provider)')(format='.')
:marked
It's that simple because the most common injected service is an instance of a class.
@@ -1186,7 +1231,7 @@ a(id='alex')
and add that provider to the `providers` array of the `@Component` metadata for the `AlexComponent`:
我们编写一个[*别名供应商*](#useexisting) —一个拥有`useExisting`定义的`provide`函数 —
- 它新建一个*可选的*方法来注入一样的组件实例,并添加这个供应商到`AlexComponent`的`@Component`元数据里的`供应商`数组。
+ 它新建一个*可选的*方法来注入一样的组件实例,并添加这个供应商到`AlexComponent`的`@Component`元数据里的`providers`数组。
a(id="alex-providers")
+makeExample('cb-dependency-injection/ts/app/parent-finder.component.ts','alex-providers','parent-finder.component.ts (AlexComponent providers)')(format='.')
:marked
@@ -1229,7 +1274,7 @@ a(id="parent-tree")
*Barry*'s `providers` array looks just like [*Alex*'s](#alex-providers).
If we're going to keep writing [*alias providers*](#useexisting) like this we should create a [helper function](#provideparent).
- *Barry*的`供应商`数组看起来像[*Alex*的](#alex-providers).
+ *Barry*的`providers`数组看起来像[*Alex*的](#alex-providers).
如果我们一直要像这样编写[*别名供应商*](#useexisting)的话,我们应该建立一个[帮助函数](#provideparent)。
For now, focus on *Barry*'s constructor:
@@ -1411,7 +1456,7 @@ a(id="forwardref")
appear *above* the class definition.
当一个类使用*一个自己的引用*的时候,我们面临同样的窘境,就像`AlexComponent`的`provdiers`数组里的困境一样。
- 该`供应商`数组是一个`@Component`装饰器函数的一个属性,它必须要在类定义*之前*出现。
+ 该`providers`数组是一个`@Component`装饰器函数的一个属性,它必须要在类定义*之前*出现。
Again we break the circularity with `forwardRef`: