当元素处于事件状态时,我使用一个简单的指令来聚焦文本输入,使用 *ngIf
。这包含在 *ngFor
循环中。
当第一个 *ngIf
被激活时,输入会按预期获得焦点。当另一个输入被激活时,我得到错误:
异常:尝试使用脱水检测器。
我不明白这是什么意思,也不知道如何防止错误发生。即使出现错误,该功能仍然有效。
@Directive({
selector: '[myAutoFocus]'
})
export class AutoFocusDirective {
constructor(private elem: ElementRef) {
window.setTimeout(function() {
elem.nativeElement.querySelector('input').focus();
});
}
}
```
最佳答案
Dehydrated detector 是已从变化检测系统中删除的组件,通常是因为它已被 *ngIf
或其他 means 从 DOM 中卸载:
如果应用程序中有一个异步操作命中这个已经卸载的检测器,则错误为 thrown :
解决方案是在受影响的组件上使用 [hidden]
而不是 *ngIf
,或者使用 setTimeout( () => this. offendingAction(), 0)
关于angular - 什么是脱水检测器,我如何在这里使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515254/