首页 文章

angular 2从@ViewChild组件重新渲染父组件

提问于
浏览
3

我正在开发angular2应用程序,其中我有一个弹出子组件的父组件

@ViewChild(childPopupComponent) case: childPopupComponent;

我想要做的是:当从子组件(在父组件内弹出)中按下保存按钮时,重新渲染父组件以进行反射(而不是重新加载整个页面) .

我知道这会重新渲染当前组件,但是如何从 @ViewChild 中重新渲染

this.ref.detectChanges();

1 回答

  • 2

    在子组件中注入 ChangeDetectorRef ,如:

    export class childPopupComponent {
        constructor(public cdRef: ChangeDetectorRef) {}
    

    之后,您将只能在子组件上循环运行更改检测:

    @ViewChild(childPopupComponent) case: childPopupComponent;
    
    this.case.cdRef.detectChanges();
    

    要更新父级,您可以将其注入子级

    export class childPopupComponent {
        constructor(public parent: ParentComponent) {}
    

    然后

    this.parent.cdRef.detectChanges()
    

    甚至试试这个:

    import { ChangeDetectorRef, SkipSelf } from '@angular/core';
    
    export class childPopupComponent {
        constructor(@SkipSelf() private parentCdRef: ChangeDetectorRef) {}
    

相关问题