How to re-render a component manually Angular 5

后端 未结 5 1333
耶瑟儿~
耶瑟儿~ 2020-12-31 01:38

Is there a way I can re render a component manually, say when a user clicks a button??

I\'ve seen similar posts but none of these worked for me for example here

5条回答
  •  情深已故
    2020-12-31 02:17

    I tried the answers in this thread but I had to do modifications to make it work. Let me share my working code.

    .html-----------------
    
    
    
    
    
    
       
    
    
    .ts---------------------
    import { Component, ViewChild, ViewContainerRef, TemplateRef, ViewRef } from '@angular/core';
    
    export class ParentComponent{
    
    @ViewChild('vc', { read: ViewContainerRef }) vc: ViewContainerRef;
    @ViewChild('tpl', { read: TemplateRef }) tpl: TemplateRef;
    
    addChild(){
    let view = this.tpl.createEmbeddedView(null);
    this.vc.insert(view);
    }
    
    removeChild(){
     this.vc.clear();
    }
    

    This page was helpful. Make sure to add let view = this.tpl.createEmbeddedView(null); in addchild()

提交回复
热议问题