Directives

About

guide/directives

They are classes that add additional behavior to elements in your applications.

Use Angular's built-in directives to manage forms, lists, styles and what users see.

Directives are type of directive that have Templates.

Best Practices

  • Avoid changing elements directly through the DOM like *.nativeElement.style.*, since angular is able to render the Templates without a DOM, for instance when using service workers, then these properties may not be available.

Attribute Directives

Change the appearance or behavior of an element, component, or another directive.

They listen to and modify the behavior of other HTML elements, attributes, properties, and components.

Built-in Attribute Directives

Built-in directives use only public APIs.

They do not have special access to any private APIs that other directives can't access.

NgClass

An old way of adding and removing CSS classes in elements.

NgStyle

An old way of adding and removing HTML styles in elements.

NgModel

Use it to display a data property and update that property when the user makes changes. (Adds two-way data binding to HTML form elements)

More examples in Examples.

To apply [(ngModel)] to a non-form built-in element or a thid-party custom component, you have to write a value accessor.

Create the new directive with the CLI command.

By default a directive will have a [appDirective] attribute seletor, but it could be a class or component selector as well.

Inject ElementRef to get access to the host DOM element.

Passing values into an attribute directive

Pass values to the Directive with input() using the same selector name as the variable name.

Handling user events

You can handle user events or subscribe to event of the DOM with @HostListener().

Structural Directives

Change the DOM layout by adding and removing DOM elements.

Structural directives can be raw applied to <ng-template> only.

Angular expanded the use of them to other HTML elements by the use of shorthand syntax (*).

Angular transforms the * into a <ng-template> that hosts the directive and surrounds the element and its descendants.

Built-in Structural Directives

Conditionally creates or disposes of subviews from the template. (Frees up memory and resources)

By default, ngIf prevents display of an element bound to a null value.

To use an else block, must use a <ng-template> block with the help of a LocalReference.

Or with a more descriptive way.

NgFor

Repeat a node for each item in a list.

You may pass the item value to a sub component like.

You can get the list index with let i=index like.

You can track items with trackBy.

Angular can change and re-render only those items that have changed, rather than reloading the entire list of items.

The item list must have some sort of unique id field.

NgSwitch

To display one element among several possible elements, based on a switch condition.

Create the new directive with the CLI command.

When you write your own structural directives, use the following syntax.

Adding Directives to a Component with hostDirectives

You can assign directives to a component through the hostDirectives property inside the component's decorator.

By default, host directive inputs and outputs are not exposed as part of the component's public API.

Execution order

Including inputs and outputs

By explicitly specifying the inputs and outputs, consumers of the component with hostDirective can bind them in a template.

Alias the inputs and outputs

You can also alias them to customize the API of your component.

Adding Directives to other Directives with hostDirectives

You can also add hostDirectives to other directives, in addition to components.

This enables the transitive aggregation of multiple behaviors (chainned directives).

Last updated