Angular2: Is one time mandatory for * ngIf available?

I am creating a DOM structure and I only want to add some parts to some visual components for the first time and I do not want them to be updated again and again, which is the way * ngIf. This is done in order to avoid binding to execution again and again, because what I know will never change after creation. In other words, Angular1 has an operator ::that helps achieve this.

Is there one binding for *ngIfin Angular2? Please point me to a question if this is being discussed in some other question.

+4
source share
1 answer

, DOM , :

import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';

@Directive({ selector: '[customNgIf]' })
export class CustomNgIfDirective {

    @Input("customNgIf") condition: boolean;

    constructor(
        private templateRef: TemplateRef<any>,
        private viewContainer: ViewContainerRef) {
    }

    ngOnInit() {
        if (condition) {
            this.viewContainer.createEmbeddedView(this.templateRef);
        } else {
            this.viewContainer.clear();
        }
    }
}

:

<div *customNgIf="expression">
    Test
</div>
+1

Source: https://habr.com/ru/post/1673319/


All Articles