On this page
Component
decorator
Decorator that marks a class as an Angular component and provides configuration metadata that determines how the component should be processed, instantiated, and used at runtime.
Option | Description |
---|---|
changeDetection |
The change-detection strategy to use for this component. |
viewProviders |
Defines the set of injectable objects that are visible to its view DOM children. See example. |
moduleId |
The module ID of the module that contains the component. The component must be able to resolve relative URLs for templates and styles. SystemJS exposes the |
templateUrl |
The URL of a template file for an Angular component. If provided, do not supply an inline template using |
template |
An inline template for an Angular component. If provided, do not supply a template file using |
styleUrls |
One or more URLs for files containing CSS stylesheets to use in this component. |
styles |
One or more inline CSS stylesheets to use in this component. |
animations |
One or more animation |
encapsulation |
An encapsulation policy for the template and CSS styles. One of:
|
interpolation |
Overrides the default encapsulation start and end delimiters ( |
entryComponents |
A set of components that should be compiled along with this component. For each component listed here, Angular creates a |
preserveWhitespaces |
True to preserve or false to remove potentially superfluous whitespace characters from the compiled template. Whitespace characters are those matching the |
Inherited from Directive decorator
Option | Description |
---|---|
selector |
The CSS selector that identifies this directive in a template and triggers instantiation of the directive. |
inputs |
Enumerates the set of data-bound input properties for a directive |
outputs |
The set of event-bound output properties. When an output property emits an event, an event handler attached to that event in the template is invoked. |
providers |
Configures the injector of this directive or component with a token that maps to a provider of a dependency. |
exportAs |
The name or names that can be used in the template to assign this directive to a variable. For multiple names, use a comma-separated string. |
queries |
Configures the queries that will be injected into the directive. |
jit |
If true, this directive/component will be skipped by the AOT compiler and so will always be compiled using JIT. |
host |
Maps class properties to host element bindings for properties, attributes, and events, using a set of key-value pairs. |
Description
Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components.
Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated per an element in a template.
A component must belong to an NgModule in order for it to be available to another component or application. To make it a member of an NgModule, list it in the declarations
field of the @NgModule
metadata.
Note that, in addition to these options for configuring a directive, you can control a component's runtime behavior by implementing life-cycle hooks. For more information, see the Lifecycle Hooks guide.
Options
changeDetection
|
---|
The change-detection strategy to use for this component. |
|
When a component is instantiated, Angular creates a change detector, which is responsible for propagating the component's bindings. The strategy is one of:
|
viewProviders
|
---|
Defines the set of injectable objects that are visible to its view DOM children. See example. |
|
moduleId
|
---|
The module ID of the module that contains the component. The component must be able to resolve relative URLs for templates and styles. SystemJS exposes the |
|
templateUrl
|
---|
The URL of a template file for an Angular component. If provided, do not supply an inline template using |
|
template
|
---|
An inline template for an Angular component. If provided, do not supply a template file using |
|
styleUrls
|
---|
One or more URLs for files containing CSS stylesheets to use in this component. |
|
styles
|
---|
One or more inline CSS stylesheets to use in this component. |
|
animations
|
---|
One or more animation |
|
encapsulation
|
---|
An encapsulation policy for the template and CSS styles. One of:
|
|
If not supplied, the value is taken from If the policy is set to |
interpolation
|
---|
Overrides the default encapsulation start and end delimiters ( |
|
entryComponents
|
---|
A set of components that should be compiled along with this component. For each component listed here, Angular creates a |
|
preserveWhitespaces
|
---|
True to preserve or false to remove potentially superfluous whitespace characters from the compiled template. Whitespace characters are those matching the |
|
Usage notes
Setting component inputs
The following example creates a component with two data-bound properties, specified by the inputs
value.
@Component({
selector: 'app-bank-account',
inputs: ['bankName', 'id: account-id'],
template: `
Bank Name: {{ bankName }}
Account Id: {{ id }}
`
})
export class BankAccountComponent {
bankName: string|null = null;
id: string|null = null;
// this property is not bound, and won't be automatically updated by Angular
normalizedBankName: string|null = null;
}
@Component({
selector: 'app-my-input',
template: `
<app-bank-account
bankName="RBC"
account-id="4747">
</app-bank-account>
`
})
export class MyInputComponent {
}
Setting component outputs
The following example shows two event emitters that emit on an interval. One emits an output every second, while the other emits every five seconds.
@Directive({selector: 'app-interval-dir', outputs: ['everySecond', 'fiveSecs: everyFiveSeconds']})
export class IntervalDirComponent {
everySecond = new EventEmitter<string>();
fiveSecs = new EventEmitter<string>();
constructor() {
setInterval(() => this.everySecond.emit('event'), 1000);
setInterval(() => this.fiveSecs.emit('event'), 5000);
}
}
@Component({
selector: 'app-my-output',
template: `
<app-interval-dir
(everySecond)="onEverySecond()"
(everyFiveSeconds)="onEveryFiveSeconds()">
</app-interval-dir>
`
})
export class MyOutputComponent {
onEverySecond() { console.log('second'); }
onEveryFiveSeconds() { console.log('five seconds'); }
}
Injecting a class with a view provider
The following simple example injects a class into a component using the view provider specified in component metadata:
class Greeter {
greet(name:string) {
return 'Hello ' + name + '!';
}
}
@Directive({
selector: 'needs-greeter'
})
class NeedsGreeter {
greeter:Greeter;
constructor(greeter:Greeter) {
this.greeter = greeter;
}
}
@Component({
selector: 'greet',
viewProviders: [
Greeter
],
template: `<needs-greeter></needs-greeter>`
})
class HelloWorld {
}
© 2010–2019 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v6.angular.io/api/core/Component