2015-04-14 02:03:13 -04:00
2015-04-22 11:06:51 -04:00
p.location-badge.
exported from <a href="/angular2/annotations">angular2/annotations</a>
2015-04-22 02:34:30 -04:00
defined in <a href="https://github.com/angular/angular/tree/master/modules/angular2/src/core/annotations/annotations.js#L521">angular2/src/core/annotations/annotations.js (line 521)</a>
2015-04-19 16:53:18 -04:00
:markdown
Declare reusable UI building blocks for an application.
Each Angular component requires a single `@Component` and at least one `@View` annotation. The `@Component`
annotation specifies when a component is instantiated, and which properties and hostListeners it binds to.
When a component is instantiated, Angular
- creates a shadow DOM for the component.
- loads the selected template into the shadow DOM.
2015-04-20 16:57:43 -04:00
- creates a child <a href="angular2/di/Injector-class"><code>Injector</code></a> which is configured with the `injectables` for the <a href="angular2/annotations/Component-class"><code>Component</code></a>.
2015-04-19 16:53:18 -04:00
All template expressions and statements are then evaluated against the component instance.
2015-04-20 16:57:43 -04:00
For details on the `@View` annotation, see <a href="angular2/annotations/View-class"><code>View</code></a>.
2015-04-19 16:53:18 -04:00
## Example
```
@Component({
selector: 'greet'
})
@View({
template: 'Hello {{name}}!'
})
class Greet {
name: string;
constructor() {
this.name = 'World';
}
}
```
2015-04-14 02:03:13 -04:00
.l-main-section
2015-04-19 16:53:18 -04:00
h2 Members
2015-04-14 02:03:13 -04:00
.l-sub-section
2015-04-19 16:53:18 -04:00
h3 constructor
2015-04-14 02:03:13 -04:00
2015-04-19 16:53:18 -04:00
pre.prettyprint
2015-04-14 02:03:13 -04:00
code.
2015-04-20 16:57:43 -04:00
constructor({
2015-04-19 16:53:18 -04:00
selector,
properties,
events,
hostListeners,
injectables,
lifecycle,
changeDetection = DEFAULT
}:{
selector:string,
properties:Object,
events:List,
hostListeners:Object,
injectables:List,
lifecycle:List,
changeDetection:string
}={})
:markdown
2015-04-14 02:03:13 -04:00
.l-sub-section
2015-04-19 16:53:18 -04:00
h3 changeDetection
2015-04-14 02:03:13 -04:00
2015-04-19 16:53:18 -04:00
:markdown
Defines the used change detection strategy.
When a component is instantiated, Angular creates a change detector, which is responsible for propagating
the component's bindings.
The `changeDetection` property defines, whether the change detection will be checked every time or only when the component
tells it to do so.
2015-04-14 02:03:13 -04:00
.l-sub-section
2015-04-19 16:53:18 -04:00
h3 injectables
:markdown
Defines the set of injectable objects that are visible to a Component and its children.
2015-04-20 16:57:43 -04:00
The `injectables` defined in the Component annotation allow you to configure a set of bindings for the component's
2015-04-19 16:53:18 -04:00
injector.
When a component is instantiated, Angular creates a new child Injector, which is configured with the bindings in
2015-04-20 16:57:43 -04:00
the Component `injectables` annotation. The injectable objects then become available for injection to the component
2015-04-19 16:53:18 -04:00
itself and any of the directives in the component's template, i.e. they are not available to the directives which
are children in the component's light DOM.
2015-04-20 16:57:43 -04:00
The syntax for configuring the `injectables` injectable is identical to <a href="angular2/di/Injector-class"><code>Injector</code></a> injectable configuration.
See <a href="angular2/di/Injector-class"><code>Injector</code></a> for additional detail.
2015-04-19 16:53:18 -04:00
## Simple Example
Here is an example of a class that can be injected:
```
class Greeter {
greet(name:string) {
return 'Hello ' + name + '!';
}
2015-04-14 02:03:13 -04:00
}
2015-04-19 16:53:18 -04:00
@Component({
selector: 'greet',
injectables: [
Greeter
]
2015-04-14 02:03:13 -04:00
})
2015-04-19 16:53:18 -04:00
@View({
template: `{{greeter.greet('world')}}!`,
directives: Child
2015-04-14 02:03:13 -04:00
})
2015-04-19 16:53:18 -04:00
class HelloWorld {
greeter:Greeter;
constructor(greeter:Greeter) {
this.greeter = greeter;
2015-04-14 02:03:13 -04:00
}
}
2015-04-19 16:53:18 -04:00
```
2015-04-14 02:03:13 -04:00