【发布时间】:2018-11-26 10:06:42
【问题描述】:
如何捕捉对 div 的更改?一个设置了 contenteditable 的 div,并且有 [hidden] 元素有时会变得可见。此单页应用布局不应滚动,而应停留在可视窗口区域中。
我正在使用 angular-split 在其中一个组件中设置可调整的分区。在添加内容或将内容更改为使 div 高度扩展的拆分之一之前,一切都正常工作并且看起来应该如此。当它扩展超出窗口的底部边缘时,周围的拆分会破裂。整个窗口没有设定高度,可以调整大小。将 CSS 与 'overflow: auto' 结合使用,div 仍然会扩展。
我正在尝试捕捉对该 div 的所有更改,以便手动计算并设置正确的高度。
html:
<div id="detail_container" [ngClass]="infoDisplay" #watchThis>
component.ts:
@ViewChild("watchThis") watchthis: ElementRef;
ngAfterViewInit() {
this.observer = new MutationObserver(mutations => {
mutations.forEach(function(mutation) {
console.log(mutation.type);
//height adjustments
});
});
var config = { attributes: true, childList: true, characterData: true };
this.observer.observe(this.watchthis.nativeElement, config);
}
没有错误,但也没有 console.logs。
【问题讨论】:
-
你能分享一个 plunkr 吗?
-
MutationObserver 观察 DOM 的变化。您可能需要 ResizeObserver 或 IntersectionObserver。另外,希望您知道,如果没有
subtree: true,您只会观察到 this.watchthis.nativeElement 的直接子节点。 -
@raviteja.kvns 也许您的代码中的其他逻辑有问题。能否请您在stackblitz.com 中提供一个最小的工作代码来展示您的问题
标签: html angular mutation-observers