Angular 2+ one-time binding

前端 未结 6 1909
故里飘歌
故里飘歌 2020-12-09 14:21

In angular 1 we could do one time binding in this way: {{ ::myFunction() }}.

In angular 2 this is throwing:

EXCEPTION: Template parse er         


        
6条回答
  •  無奈伤痛
    2020-12-09 15:16

    I found solution for one time binding in angular 2 here: https://github.com/angular/angular/issues/14033

    I created this directive:

     import { Directive, TemplateRef, ViewContainerRef, NgZone } from "@angular/core";
    
    @Directive({
        selector: '[oneTime]',
    })
    export class OneTimeDirective {
        constructor(template: TemplateRef, container: ViewContainerRef, zone: NgZone) {
            zone.runOutsideAngular(() => {
                const view = container.createEmbeddedView(template);
                setTimeout(() => view.detach());
            })
        }
    }
    

    And used it:

      
    

    For example:

         
    

提交回复
热议问题