Creating structural directives

We have already seen how to use structural directives from our previous examples. How do we create them? We create them in the same way we created the attribute directive, by running the following command in your terminal:

ng generate directive when

Yes, we are naming the directive when. This directive does exactly what *ngIf does, so, hopefully, it will help you better understand the internals of the directive you've already used.

Update the directive with the following:

import {   Directive,   Input,   TemplateRef,   ViewContainerRef } from '@angular/core';@Directive({  selector: '[appWhen]'})export class WhenDirective {  constructor(    private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef) ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.