[英]Angular2 - Do something when innerHTML changed
I have a component and its inner html
is dynamically changing. 我有一个组件,它的
inner html
是动态变化的。 I do some operation on the child DOM elements of this component, so I have to repeat the operations each time the inner html
of this component is changed. 我对该组件的子DOM元素进行了一些操作,因此,每次更改此组件的
inner html
时,我都必须重复操作。
For example in the component: 例如在组件中:
myElements = this.el.nativeElement.querySelectorAll('.myClass');
// do something with that DOM elements
And in the view: 并在视图中:
<div *ngFor="let item of myItems">
<div class="myClass">...</div>
</div>
I have to call 我必须打电话
myElements = this.el.nativeElement.querySelectorAll('.myClass');
Each time myItems is updated and new DOM elements loaded. 每次更新myItems并加载新的DOM元素。
Can I bind the innerHTML
to ngOnChange()
or is there other ways to achieve this? 我可以将
innerHTML
绑定到ngOnChange()
还是有其他方法来实现?
You could try ngDoCheck: 您可以尝试ngDoCheck:
https://angular.io/docs/ts/latest/api/core/index/DoCheck-class.html https://angular.io/docs/ts/latest/api/core/index/DoCheck-class.html
You would manually check the old value and the new value of the innerHtml, then execute your custom code. 您将手动检查innerHtml的旧值和新值,然后执行自定义代码。
Potential example: 潜在的例子:
public ngDoCheck(){
if(this.oldInnerHtml != this.newInnerHtml){
//take custom action
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.