Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a dehydrated detector and how am I using one here?

Tags:

angular

I'm using a simple directive to focus a text input when the element is active using *ngIf. This is contained within a *ngFor loop.

When the first *ngIf is activated, the input focuses as expected. When another input is activated I get the error:

EXCEPTION: Attempt to use a dehydrated detector.

I don't understand what this means and how I can prevent the error. The functionality still works even with the error.

@Directive({
    selector: '[myAutoFocus]'
})
export class AutoFocusDirective {
    constructor(private elem: ElementRef) {
        window.setTimeout(function() {
            elem.nativeElement.querySelector('input').focus();
        });
    }
}

```

like image 616
Fisu Avatar asked Feb 19 '16 21:02

Fisu


1 Answers

Dehydrated detector is a component that has been removed from the change detection system, usually because it has been unmounted from DOM by *ngIf or other means: If there's an asynchronous action in the application that hits this already unmounted detector, error is thrown: solution is to use [hidden] instead of *ngIf on the affected component, or defer the offending action to next tick using setTimeout( () => this.offendingAction(), 0)

like image 141
Tomas Holas Avatar answered Nov 15 '22 23:11

Tomas Holas