Angular 4 Changes based on view

情到浓时终转凉″ 提交于 2019-12-12 06:02:22

问题


I have a div which is like

 <div [ngClass]="{'active' : isActive} [ngStyle]={top: topVar } >

isActive and topVar are calculated based on the view, (like if the content overflowed, and the content is dynamic).

so I calculate them in ngAfterViewChecked.

which ofcourse gives me this error :

Expression has changed after it was checked, previous value : "undefined". current value : '636px'.

I understand what I did wrong, my question is, how to do it right?

(If I calculate the the vars based on the component view, I need ngAfterViewChecked --> I need to detectChanged again, and if I will make ChangeDetectorRef.detectChanges() everytime --> i will enter a loop. --> I need a condition to stop it. that feels hacky and wrong.


回答1:


Wrap your the code into setTimeout function. Like

ngAfterViewInit(): void {
  setTimeout({...calculation});
}


来源:https://stackoverflow.com/questions/44653504/angular-4-changes-based-on-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!