Event to fire when an angular *ngIf statement evaluates in template

后端 未结 2 1251
春和景丽
春和景丽 2020-12-18 22:32

If I have the following:

{{user.name}}&l

2条回答
  •  执笔经年
    2020-12-18 23:17

    The *ngIf will remove that DOM element and all attached components/directives. So you can just write a simple directive that executes an event when it's first created. When the *ngIf transitions from false to true the directive will be created (again, and again, etc...)

    @Directive({selector: '[after-if]'})
    export class AfterIfDirective implements AfterContentInit {
        @Output('after-if')
        public after: EventEmitter = new EventEmitter();
    
        public ngAfterContentInit(): void {
           // timeout helps prevent unexpected change errors
           setTimeout(()=> this.after.next());
        }
    }
    

    Sample HTML:

    {{user.name}}

提交回复
热议问题