angular-cn/public/docs/js/latest/api/annotations/Parent-class.jade

61 lines
1.3 KiB
Plaintext

p.
<span class="location-badge">exported from <a href="/angular2/annotations">angular2/annotations</a></span>
defined in <a href="https://github.com/angular/angular/tree/master/modules/angular2/src/core/annotations/visibility.js#L44">angular2/src/core/annotations/visibility.js (line 44)</a>
:markdown
Specifies that an injector should retrieve a dependency from the direct parent.
## Example
Here is a simple directive that retrieves a dependency from its parent element.
```
@Decorator({
selector: '[dependency]',
properties: {
'id':'dependency'
}
})
class Dependency {
id:string;
}
@Decorator({
selector: '[my-directive]'
})
class Dependency {
constructor(@Parent() dependency:Dependency) {
expect(dependency.id).toEqual(1);
};
}
```
We use this with the following HTML template:
```
<div dependency="1">
<div dependency="2" my-directive></div>
</div>
```
The `@Parent()` annotation in our constructor forces the injector to retrieve the dependency from the
parent element (even thought the current element could resolve it): Angular injects `dependency=1`.
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
constructor()
:markdown