Angular 2+ one-time binding

前端 未结 6 1916
故里飘歌
故里飘歌 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条回答
  •  萌比男神i
    2020-12-09 15:15

    Since one time read/bind isnt possible by default in angular, I think writing a public getter function will solve the issue.

    For example

    public getValue():number {
     return mynumber ? mynumber : 25; // if mynumber is not undefined the mynumber else return 25
    }
    
    //In html template
    
    

    This will work perfectly if the getter function is able to reply back before the template rendering takes place. So the initialization of mynumber would be great if done in ngOnInit() function

提交回复
热议问题