[英]Angular 2+: change detection, the state between property-change and view-update
-------------- update --------------- --------------更新---------------
ChangeDetectorRef.detectChanges() run the change detectors for the component and its children. ChangeDetectorRef.detectChanges()运行组件及其子组件的更改检测器。 Thus, it might one solution.
因此,它可能是一个解决方案。
-------------- original question ---------------- --------------原始问题----------------
Purpose: I want to get informed after a specific dom element is updated in a component. 目的:我希望在组件中更新特定的dom元素后获得通知。
See the picture underneath: (change the text property will change the content of p , which in turn change the height of p element in dom) 查看下面的图片:(更改文本属性将更改p的内容,从而改变dom中p元素的高度)
Result: the clientHeight of p is still the previous value, not the newest one. 结果: p的clientHeight仍然是先前的值,而不是最新的值。
Expected result : the newest value 预期结果 :最新值
possible solutions: 可能的解决方案:
4. My confusions: 我的困惑:
changeProperty(this.text => { this.text = 'A long long text'; }).then( () => { // here is the state after the corresponding text is updated in the view })
Or which is the right way to solve this problem here? 或者哪个是解决这个问题的正确方法? Appreciate for any thoughts!
感谢任何想法!
There is nothing like that and I doubt it will ever appear. 没有这样的东西,我怀疑它会出现。
However your particular problem can be solved in a totally different manner. 但是,您的特定问题可以以完全不同的方式解决。 As long as you want to control the way how your text / html gets rendered it is way easier to render it yourself.
只要你想控制text / html的渲染方式,就可以更容易地自己渲染它。
Just set the innerHTML and the proper height will be in place 只需设置innerHTML,就会有适当的高度
changeText(para) {
para.innerHTML = 'long text';
console.log(para.clientHeight);
}
and that's it. 就是这样。 Remember that it may be better to use
innerText
or perform some HTML sanitizing before assigning it like that. 请记住,在分配它之前使用
innerText
或执行一些HTML innerText
可能更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.