/* Directive code omitted for brevity */ @Component({ selector: 'app-root', template: ` <div *appUnless="condition">Show this if condition is false</div> ` }) export class AppComponent { condition = false; }
The appUnless directive shows its content only when the condition is false. Since condition is false, the content inside the directive is rendered.
Structural directives are declared with attribute selectors like [appShowIf]. In templates, they are used with the asterisk syntax *appShowIf which Angular desugars to <ng-template [appShowIf]>.
Angular calls ngOnChanges whenever an input property changes, including the first time it is set. This applies to structural directives as well.
The createEmbeddedView method belongs to ViewContainerRef, not TemplateRef. The directive should inject ViewContainerRef to create views.
- Item {{index}}
The directive creates the embedded view 3 times, passing an index context each time. The template uses {{index}} so it shows 0, 1, and 2 respectively.