Component
decorator
stable
Marks a class as an Angular component and collects component configuration metadata.
@Component({
changeDetection?: ChangeDetectionStrategy
viewProviders?: Provider[]
moduleId?: string
templateUrl?: string
template?: string
styleUrls?: string[]
styles?: string[]
animations?: any[]
encapsulation?: ViewEncapsulation
interpolation?: [string, string]
entryComponents?: Array<Type<any>|any[]>
preserveWhitespaces?: boolean
// inherited from core/Directive
selector?: string
inputs?: string[]
outputs?: string[]
host?: {[key: string]: string}
providers?: Provider[]
exportAs?: string
queries?: {[key: string]: any}
})
How To Use
@Component({selector: 'greet', template: 'Hello {{name}}!'})
class Greet {
name: string = 'World';
}
Description
Component decorator allows you to mark a class as an Angular component and provide additional metadata that determines how the component should be processed, instantiated and used at runtime.
Components are the most basic building block of an UI in an Angular application. An Angular application is a tree of Angular components. Angular components are a subset of directives. Unlike directives, components always have a template and 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 usable by another component or application. To specify that a component is a member of an NgModule, you should list it in the declarations
field of that NgModule.
In addition to the metadata configuration specified via the Component decorator, components can control their runtime behavior by implementing various Life-Cycle hooks.
Metadata Properties:
-
animations - list of animations of this component
-
changeDetection - change detection strategy used by this component
-
encapsulation - style encapsulation strategy used by this component
-
entryComponents - list of components that are dynamically inserted into the view of this component
-
exportAs - name under which the component instance is exported in a template
-
host - map of class property to host element bindings for events, properties and attributes
-
inputs - list of class property names to data-bind as component inputs
-
interpolation - custom interpolation markers used in this component's template
-
moduleId - ES/CommonJS module id of the file in which this component is defined
-
outputs - list of class property names that expose output events that others can subscribe to
-
providers - list of providers available to this component and its children
-
queries - configure queries that can be injected into the component
-
selector - css selector that identifies this component in a template
-
styleUrls - list of urls to stylesheets to be applied to this component's view
-
styles - inline-defined styles to be applied to this component's view
-
template - inline-defined template for the view
-
templateUrl - url to an external file containing a template for the view
-
viewProviders - list of providers available to this component and its view children
Example
@Component({selector: 'greet', template: 'Hello {{name}}!'})
class Greet {
name: string = 'World';
}
animations?: any[]
Animations are defined on components via an animation-like DSL. This DSL approach to describing animations allows for a flexibility that both benefits developers and the framework.
Animations work by listening on state changes that occur on an element within the template. When a state change occurs, Angular can then take advantage and animate the arc in between. This works similar to how CSS transitions work, however, by having a programmatic DSL, animations are not limited to environments that are DOM-specific. (Angular can also perform optimizations behind the scenes to make animations more performant.)
For animations to be available for use, animation state changes are placed within animation triggers which are housed inside of the animations
annotation metadata. Within a trigger both state and transition entries can be placed.
@Component({
selector: 'animation-cmp',
templateUrl: 'animation-cmp.html',
animations: [
// this here is our animation trigger that
// will contain our state change animations.
trigger('myTriggerName', [
// the styles defined for the `on` and `off`
// states declared below are persisted on the
// element once the animation completes.
state('on', style({ opacity: 1 }),
state('off', style({ opacity: 0 }),
// this here is our animation that kicks off when
// this state change jump is true
transition('on => off', [
animate("1s")
])
])
]
})
As depicted in the code above, a group of related animation states are all contained within an animation trigger
(the code example above called the trigger myTriggerName
). When a trigger is created then it can be bound onto an element within the component's template via a property prefixed by an @
symbol followed by trigger name and an expression that is used to determine the state value for that trigger.
<!-- animation-cmp.html -->
<div @myTriggerName="expression">...</div>
For state changes to be executed, the expression
value must change value from its existing value to something that we have set an animation to animate on (in the example above we are listening to a change of state between on
and off
). The expression
value attached to the trigger must be something that can be evaluated with the template/component context.
DSL Animation Functions
Please visit each of the animation DSL functions listed below to gain a better understanding of how and why they are used for crafting animations in Angular:
preserveWhitespaces?: boolean
If Component.preserveWhitespaces is set to false
potentially superfluous whitespace characters (ones matching the \s
character class in JavaScript regular expressions) will be removed from a compiled template. This can greatly reduce AOT-generated code size as well as speed up view creation.
Current implementation works according to the following rules:
- all whitespaces at the beginning and the end of a template are removed (trimmed);
- text nodes consisting of whitespaces only are removed (ex.:
<button>Action 1</button> <button>Action 2</button>
will be converted to <button>Action 1</button><button>Action 2</button>
(no whitespaces between buttons); - series of whitespaces in text nodes are replaced with one space (ex.:
<span>\n some text\n</span>
will be converted to <span> some text </span>
); - text nodes are left as-is inside HTML tags where whitespaces are significant (ex.
<pre>
, <textarea>
).
Described transformations can (potentially) influence DOM nodes layout so the preserveWhitespaces
option is true
be default (no whitespace removal). In Angular 5 you need to opt-in for whitespace removal (but we might revisit the default setting in Angular 6 or later). If you want to change the default setting for all components in your application you can use the preserveWhitespaces
option of the AOT compiler.
Even if you decide to opt-in for whitespace removal there are ways of preserving whitespaces in certain fragments of a template. You can either exclude entire DOM sub-tree by using the ngPreserveWhitespaces
attribute, ex.:
<div ngPreserveWhitespaces>
whitespaces are preserved here
<span> and here </span>
</div>
Alternatively you can force a space to be preserved in a text node by using the &ngsp;
pseudo-entity. &ngsp;
will be replaced with a space character by Angular's template compiler, ex.:
<a>Spaces</a>&ngsp;<a>between</a>&ngsp;<a>links.</a>
will be compiled to the equivalent of:
<a>Spaces</a> <a>between</a> <a>links.</a>
Please note that sequences of &ngsp;
are still collapsed to just one space character when the preserveWhitespaces
option is set to false
. Ex.:
<a>before</a>&ngsp;&ngsp;&ngsp;<a>after</a>
would be equivalent to:
<a>before</a> <a>after</a>
The &ngsp;
pseudo-entity is useful for forcing presence of one space (a text node having &ngsp;
pseudo-entities will never be removed), but it is not meant to mark sequences of whitespace characters. The previously described ngPreserveWhitespaces
attribute is more useful for preserving sequences of whitespace characters.