Directives
About
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.
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
NgClass
NgClassAn old way of adding and removing CSS classes in elements.
Preffer to use Class Binding [class.].
NgStyle
NgStyleAn old way of adding and removing HTML styles in elements.
Preffer to use Style Binding [style.].
NgModel
NgModelUse 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.
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.
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().
But prefer to use Binding to the host element.
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 (*).
You can only apply one structural directive per element when using the shorthand syntax (*).
If multiple directives need to be applied, apply them in a <ng-container>.
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
NgForRepeat 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
NgSwitchTo display one element among several possible elements, based on a switch condition.
Create the new directive with the CLI command.
Adding Directives to a Component with hostDirectives
hostDirectivesOnly usable in standalone components.
You can assign directives to a component through the hostDirectives property inside the component's decorator.
Angular applies host directives statically at compile time.
You cannot dynamically add directives at runtime.
Angular ignores the selector of directives applied in the hostDirectives property.
Execution order
Host directives always execute their constructor, lifecycle hooks, and binding before the component or directive on which they are applied.
This means that components with hostDirectives can override any host binding specified by a host directive.
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
hostDirectivesYou can also add hostDirectives to other directives, in addition to components.
This enables the transitive aggregation of multiple behaviors (chainned directives).
Last updated
