While we will often be using structural directives, such as NgIf
and NgFor
, there is seldom a need to creating a structural directive. Think carefully. If we need a new view, we create a component. If we need to extend an existing element/component, we use a directive. Whereas the most common use of structural directives is to clone a piece of a view (also called a template view) and then, based on some conditions:
- Either inject/destroy these templates (
NgIf
andNgSwitch
) - Or duplicate these templates (
NgFor
)
Any behavior implemented using structure directives will inadvertently fall into either of these two categories.
Given this fact, instead of building our own structural directive, let's look at the source code of the NgIf
implementation.
The following is an excerpt from the NgIf
directive that is of interest to us. We have ignored the ngIfElse
parts from the excerpt intentionally:
@Directive({selector: '[ngIf]'}) export class NgIf { constructor(private...