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;
  }
}

相关内容

热门资讯

4分钟辅助!德州圈脚本,wep... 4分钟辅助!德州圈脚本,wepoker透视功能下载,学习教程(竟然有挂)1、操作简单,无需wepok...
6分钟辅助!wepoker插件... 6分钟辅助!wepoker插件程序激活码,wepoker透视脚本免费app,窍门教程(有挂解密)1、...
五分钟辅助!aapoker能控... 五分钟辅助!aapoker能控制牌吗,hhpoker透视脚本,学习教程(有挂方针)1、玩家可以在aa...
三分钟辅助!智星菠萝可以辅助吗... 三分钟辅助!智星菠萝可以辅助吗,德州真人透视脚本,绝活儿教程(有挂方法)三分钟辅助!智星菠萝可以辅助...
第8分钟辅助!wepoker有... 第8分钟辅助!wepoker有没有辅助,hhpoker必备开挂,法门教程(真实有挂)1、超多福利:超...
4分钟辅助!wejoker开挂... 4分钟辅助!wejoker开挂,sohoo poker辅助器,模板教程(有挂技巧)1、首先打开soh...
第七分钟辅助!wpk模拟器,w... 第七分钟辅助!wpk模拟器,wpk透视辅助靠谱吗,妙计教程(有挂细节)在进入wpk透视辅助靠谱吗软件...
第七分钟辅助!wepoker开... 第七分钟辅助!wepoker开辅助能查到吗,aapoker辅助器怎么用,经验教程(确实有挂)1、起透...
第7分钟辅助!wepoker智... 第7分钟辅助!wepoker智能辅助插件,哈糖大菠萝开挂,妙计教程(有挂详细)1)wepoker智能...
第三分钟辅助!hhpoker视... 您好,hhpoker视频巡查真的假的这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...