简体   繁体   English

Angular 2+:更改检测,属性更改和视图更新之间的状态

[英]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 ---------------- --------------原始问题----------------

  1. 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元素的高度) 在此输入图像描述

  2. Result: the clientHeight of p is still the previous value, not the newest one. 结果: p的clientHeight仍然是先前的值,而不是最新的值。

    Expected result : the newest value 预期结果 :最新值

  3. possible solutions: 可能的解决方案:

    • I know ngAfterViewChecked will get fired every time in the change detection and it stands for the state after the view is updated. 我知道ngAfterViewChecked每次都会在更改检测中被触发,它代表视图更新后的状态。
    • Some walkaround solution for this problem can be force the change detection happens like: ApplicationRef.tick() or ChangeDetectorRef.detectChanges() (see the answer here: https://stackoverflow.com/a/34829089/7787645 ) 针对此问题的一些解决方案可以强制更改检测发生如下: ApplicationRef.tick()ChangeDetectorRef.detectChanges() (请参阅此处的答案: https//stackoverflow.com/a/34829089/7787645

4. My confusions: 我的困惑:

  • What I want is the state after a specific DOM element in a component get updated. 我想要的是在组件中的特定DOM元素更新后的状态。
  • ngAfterViewChecked will get fired by every async functions(by default), which is not suitable here I think. ngAfterViewChecked将被每个异步函数(默认情况下)触发,这在我认为不合适。
  • And ApplicationRef.tick() or ChangeDetectorRef.detectChanges() can force to trigger the change detection which will parse the whole component tree(from root to the child) which I think might be too costly. ApplicationRef.tick()ChangeDetectorRef.detectChanges()可以强制触发更改检测,它将解析整个组件树(从root到child),我认为这可能代价太高。
  • I'm wondering whether there is some function like: 我想知道是否有一些功能:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM