Angular深拷贝的子组件输入无法在UI上更改
创始人
2024-10-29 11:30:06
0

在深拷贝时,会创建一个新的对象,因此在 UI 上更改该对象的属性时,并不会影响原始对象。为了解决这个问题,请在子组件中使用 EventEmitter 来将更改传递回父组件,并在父组件中更新原始对象。

示例代码:

子组件:

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

@Component({
  selector: 'child-component',
  template: `
    
  `,
})
export class ChildComponent {
  private innerValue: any = {};

  @Input() set value(value: any) {
    this.innerValue = { ...value };
  }

  @Output() valueChange = new EventEmitter();

  onModelChange() {
    this.valueChange.emit(this.innerValue);
  }
}

父组件:

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

@Component({
  selector: 'parent-component',
  template: `
    
  `,
})
export class ParentComponent {
  originalValue = { prop1: '', prop2: '' };
  
  onValueChange(newValue) {
    // 更新原始对象
    this.originalValue = newValue;
  }
}

相关内容

热门资讯

必知教程!fishpoker透... 必知教程!fishpoker透视底牌,wpk透视辅助靠谱吗,总是是真的有挂(有挂细节)一、游戏安装教...
最新技巧!wejoker手机版... 最新技巧!wejoker手机版透视脚本,wepoker辅助器安装包,都是存在有挂(有挂攻略)1、金币...
交流学习经验!aapoker怎... 交流学习经验!aapoker怎么控制牌,哈糖大菠萝万能挂,一直真的是有挂(有挂透明挂)1、很好的工具...
一分钟了解!wepoker网页... 一分钟了解!wepoker网页版透视方法,如何下载wepoker安装包,切实有挂(有挂攻略)1、玩家...
推荐几款新版!智星德州有脚本吗... 推荐几款新版!智星德州有脚本吗,哈糖大菠萝有没有挂,真是存在有挂(确实有挂)1、操作简单,无需手机版...
一分钟了解!拱趴大菠萝辅助神器... 一分钟了解!拱趴大菠萝辅助神器,aa poker透视软件,总是是真的有挂(有挂规律)拱趴大菠萝辅助神...
重大科普!wpk辅助,soho... 重大科普!wpk辅助,sohoo poker辅助器,果然真的是有挂(详细教程)1)免费钻石:进一步探...
总算明白!德普之星透视辅助软件... 总算明白!德普之星透视辅助软件下载,约局吧开挂神器是真的吗,一直有挂(有挂秘籍)1、辅助软件下载优化...
带你了解!wepoker怎么买... 带你了解!wepoker怎么买辅助,拱趴大菠萝万能辅助器,一直真的有挂(真的有挂)1、起透看视 辅助...
信息共享!wepoker透视脚... 信息共享!wepoker透视脚本,wpk私人局辅助是真的吗,竟然真的是有挂(真实有挂)1、进入到是否...