I'm setting a timeout to destroy a component on Angular 2 that may be destroyed before the timeout is called. The timeout is called either way and it does a .remove() on the native element of the component (even though it is no longer in the dom).
If the element is destroyed and the timeout executes to remove the already destroyed component are there any negative side affects that are not visible?
export class AnimationCloserComponent {
public queryParams$;
constructor(
private router: Router,
private elementRef:ElementRef,
private activatedRoute:ActivatedRoute) {}
ngOnInit() {
/* Will look for routing instructions with QueryParams to route and close this component. These instructions may sometimes not be available.. */
this.queryParams$ = this.activatedRoute
.queryParamMap
.map(params => {
let closeOutletName = params.get('closeOutlet') || null;
if (closeOutletName != null) {
this.router.navigate(['', { outlets: { [closeOutletName]: null }}]);
}
return params.get('closeOutlet') || null;
}
);
/* This is meant to destroy the component if the router could not route away from it. */
setTimeout(()=>{
this.elementRef.nativeElement.remove();
}, 1500);
}
}
I really wanted to ask if just doing this is ok before I make this a practice. (already clarified below)
Angular ElementRef is a wrapper around a native element inside of a View. It's simply a class that wraps native DOM elements in the browser and allows you to work with the DOM by providing the nativeElement object which exposes all the methods and properties of the native elements.
According to Angular docs ElementRef : Use this API as the last resort when direct access to DOM is needed. Permitting direct access to the DOM can make your application more vulnerable to XSS attacks. Carefully review any use of ElementRef in your code.
To manipulate the DOM using the ElementRef , we need to get the reference to the DOM element in the component/directive. Create a template reference variable for the element in the component/directive. Use the template variable to inject the element into component class using the ViewChild or ViewChildren.
nativeElement. Angular will give you the reference of your component which rendered on DOM. It means Angular gives a plain javascript object and you can play with this object at any browser.
It is almost never OK to remove native DOM elements without Angular knowing about it. Angular keeps all component related nodes (including child components) in the abstraction called View. Nodes in the view point to DOM elements. Consider the following setup:
ComponentA
ComponentB
the view hierarchy will be something like this:
ComponentAView
ElementNode('<b-comp>', document.createElement('<b-comp>'))
ComponentBView
...
ComponentClassNode(new ComponentB());
If you remove the first element <b-comp>
from the DOM Angular knows nothing about it. It will continue thinking that there's a child component available.
This may result in the unexpected consequences like Angular reporting a child component in the @ViewChildren
while you have removed it from the DOM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With