Every Component in Angular can be thought of as a Directive with its own View.
Implications
Because only components have views, this has several consequences, such as:
- Only components are allowed to define
directives
for use within the component and its entire subtree.
- Only components are permitted to define
pipes
for use within the component and its entire subtree.
- Only components have the ability to define
viewEncapsulation
since they possess views, unlike directives.
- When the framework generates an
ElementInjector
for a specific component, it is designated as a Host
injector.
- A distinct instance of the change detector will be created exclusively for components (and consequently only components can specify the change detection strategy).
More Information
In Angular 2, the traditional approach to defining a component looks like this:
@Component({
selector: '...',
// ...
})
@View({
template: '...'
})
class ComponentCtrl {...}
The @View
decorator allows you to define a view for a particular component. Originally, it was separated into a distinct decorator (similar to the example above) because the Angular team intended to enable a single component to have multiple view definitions (one for each platform on which the component would operate).
However, this decorator has since been removed, so now you can define a component using the following syntax:
@Component({
selector: '...',
template: '...',
//...
})
class ComponentCtrl {...}
This method produces the same outcome but requires less typing. Angular 2 internally adds the necessary view metadata based on the properties set in the @Component
decorator.