gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:30:34 24 4
gpt4 key购买 nike

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

@ViewChild(childPopupComponent) case: childPopupComponent;

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

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

this.ref.detectChanges();

最佳答案

在您的子组件中注入(inject) ChangeDetectorRef,例如:

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

然后您将能够仅在子组件上循环运行更改检测:

@ViewChild(childPopupComponent) case: childPopupComponent;

this.case.cdRef.detectChanges();

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

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) {}

关于angular 2从@ViewChild组件重新渲染父组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com