Directives
Last updated
Was this helpful?
Last updated
Was this helpful?
A directive is a class with a @Directive
decorator.
There are two kinds of directives besides components: structural and attribute directives. You can apply many attribute directives to one host element. You can only apply one structural directive to a host element.
Just as for components, the metadata for a directive associates the class with a selector that you use to insert it into HTML. In templates, directives typically appear within an element tag as attributes, either by name or as the target of an assignment or a binding.
A component is technically a directive. It's a directive with a template.
Components are so distinctive and central to Angular applications that Angular defines the @Component decorator, which extends the @Directive decorator with template-oriented features.
Structural directives alter layout by adding, removing, and replacing elements in DOM. The example template uses two built-in structural directives to add application logic to how the view is rendered:
You can only apply one structural directive to an element.
Use
<ng-container>
to group elements when there is no suitable host element for the directive, or when you need both an *ngFor and an *ngIf on the same host elementStructural directives can be desugared into the <ng-template> element form. See
*ngFor
is an iterative; it tells Angular to stamp out one <li> per hero in the heroes list.
*ngIf
is a conditional
The ngIf
directive doesn't hide elements with CSS. It adds and removes them physically from the DOM.
it includes the HeroDetail component only if a selected hero exists.
*ngSwitch
Attribute directives alter the appearance or behavior of an existing element. In templates they look like regular HTML attributes, hence the name.
Built-in attribute directives
ngModel
two-way data binding:
is an example of an attribute directive. ngModel modifies the behavior of an existing element (typically an <input>) by setting its display value property and responding to change events.
The <ng-container> is a syntax element recognized by the Angular parser.
It's not a directive, component, class, or interface.
[R]
[R]
[R]
[R]