Angular的子组件在父组件的值发生更改时不会变化。
创始人
2024-10-24 11:32:21
0

可能是因为子组件没有及时更新其值,为了解决这个问题,可以使用@Input装饰器以在子组件中获取和响应父组件被传递的数据。通过这种方式,可能会出现子组件自动更新其值的情况。示例如下:

父组件HTML:

父组件TS:

myValue: string = 'Hello World!';

子组件TS:

import { Component, Input } from '@angular/core';

@Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css'] }) export class ChildComponent { @Input() parentValue: string; constructor() { } }

子组件HTML:

Parent Value: {{parentValue}}

现在,每当父组件的值更改时,都会相应地在子组件中更新其值。

如果上述方法仍未解决问题,那么可以尝试使用Angular的变更检测策略来强制更新子组件,如下所示:

父组件TS:

import { Component, ChangeDetectorRef } from '@angular/core';

@Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: ['./parent.component.css'] }) export class ParentComponent { myValue: string = 'Hello World!'; constructor(private cd: ChangeDetectorRef) {}

updateValue() { this.myValue = 'New Value!'; this.cd.detectChanges(); } }

此时,每次调用cd.detectChanges()时都会强制更新子组件值。

相关内容

热门资讯

热点推荐!hhpoker是内部... 热点推荐!hhpoker是内部控制吗(辅助挂)都是是真的有挂(果真有挂辅助app)能透视中分为三种模...
技术分享!wpk透视工作室(辅... 技术分享!wpk透视工作室(辅助挂)果然是有挂(新版有挂辅助方法)1、首先打开辅助器下载最新版本,在...
揭秘几款!wejoker透视方... 揭秘几款!wejoker透视方法(辅助挂)确实是真的有挂(有挂方法辅助教程)1、每一步都需要思考,不...
终于清楚!hhpoker软件安... 终于清楚!hhpoker软件安装包(辅助挂)总是存在有挂(有挂详细辅助攻略)1、起透看视 辅助软件价...
今日百科!哈糖大菠萝怎么开挂(... 今日百科!哈糖大菠萝怎么开挂(辅助挂)切实真的是有挂(有挂教学辅助器)1、破解器简单,机器人操作,插...
揭秘!德扑圈透视(辅助挂)其实... 揭秘!德扑圈透视(辅助挂)其实真的是有挂(有挂方法辅助器)1. 选牌创建新账号,点击进入游戏后,点击...
技术分享!We poker辅助... 技术分享!We poker辅助器下载(辅助挂)一直有挂(有挂教学辅助教程)破解侠是真的助透视。每个模...
2分钟细说!hhpoker脚本... 2分钟细说!hhpoker脚本(辅助挂)好像真的是有挂(有挂攻略辅助插件)1、2分钟细说!hhpok...
六分钟了解!hhpoker免费... 六分钟了解!hhpoker免费透视脚本(辅助挂)原来是有挂(详细教程辅助方法)一、游戏安装教程牌型概...
今日科普!pokerworld... 今日科普!pokerworld修改器(辅助挂)好像是有挂(有挂解惑辅助教程)1、用户打开应用后不用登...