How to re-render a component manually Angular 5

后端 未结 5 1324
耶瑟儿~
耶瑟儿~ 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:04

    If you meant to manipulate the view (add, remove or reattach) then here is an example:

    import { Component, ViewContainerRef, AfterViewInit, ViewChild, ViewRef,TemplateRef} from '@angular/core';
    
    import { ChildComponent } from './child.component';
    
    @Component({
      selector: 'host-comp',
      template: `
        

    I am a host component


    ` }) export class HostComponent implements AfterViewInit{ @ViewChild('vc', {read: ViewContainerRef}) vc: ViewContainerRef; @ViewChild('tpl', {read: TemplateRef}) tpl: TemplateRef; childViewRef: ViewRef; constructor(){} ngAfterViewInit(){ this.childViewRef = this.tpl.createEmbeddedView(null); } insertChildView(){ this.vc.insert(this.childViewRef); } removeChildView(){ this.vc.detach(); } reloadChildView(){ this.removeChildView(); setTimeout(() =>{ this.insertChildView(); }, 3000); } }

    live example here

提交回复
热议问题