3

I have a service, which create new blade and save it in blades array. I show this blade using "DynamicComponentLoader". Now i want to delete my blade, from another blade. But how i can do that?

2

1 Answer 1

6
export class YourComponent {
  constructor(private ref:ElementRef) {}

  someFunc() {
    elementRef.nativeElement.querySelector('some-elem').destroy();
  }
}

You could also use a wrapper element

<div #wrapper><dynamic-component></dynamic-component>

then use

@ViewChild('wrapper') wrapper;
...
someFunc() {
  while (myNode.firstChild) {
    myNode.removeChild(myNode.firstChild);
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Günter Zöchbauer. But this is delete from DOM. For example, if i have ngOnDestroy() in component, it doesnt perform. Angular dont know that the component was deleted.
@АлексейСерафимов: You could use the approach described in stackoverflow.com/q/34093670/4715679 – i.e.: pass a reference to the ComponentRef to the created component and invoke the dispose() method on it.
I was wondering if .remove() is the same as display: none or just removes a rendered element
@Mese display: none and remove() (now destroy()) are entirely different. display: none is CSS only and just makes the host element invisible and can made be visible by for example setting display: block, while destroy() destroys the component, removes the host element from the DOM and only can be re-added by using ViewContainerRef.createComponent().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.