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

相关内容

热门资讯

透视窍门!wepoker透视挂... 透视窍门!wepoker透视挂底牌(WePoKer高级)一直真的有辅助脚本(哔哩哔哩)1、wepok...
透视指南!红龙poker辅助(... 透视指南!红龙poker辅助(HHpoker模拟器)竟然存在有辅助神器(哔哩哔哩)1、下载好红龙po...
透视经验!hhpoker有没有... 透视经验!hhpoker有没有外开挂(WePoKer辅助挂)总是真的有辅助教程(哔哩哔哩)1、hhp...
透视诀窍!wpk俱乐部是真的吗... 透视诀窍!wpk俱乐部是真的吗(WPK安装)一贯是有辅助方法(哔哩哔哩)1、上手简单,内置详细流程视...
透视练习!hhpoker是正规... 透视练习!hhpoker是正规的吗(WePoKer安卓)果然是真的有辅助方法(哔哩哔哩)1、上手简单...
透视大纲!wepoker数据分... 透视大纲!wepoker数据分析工具(HHpoker有辅助)好像真的是有辅助插件(哔哩哔哩)1、每一...
透视手段!德普之星透视辅助插件... 透视手段!德普之星透视辅助插件(WePoKer价格)真是真的有辅助插件(哔哩哔哩)1、完成德普之星透...
透视绝活!fishpoker透... 透视绝活!fishpoker透视底牌(AApoker ai)果然真的有辅助app(哔哩哔哩)1、在f...
透视要领!xpoker辅助怎么... 透视要领!xpoker辅助怎么用(HHpoker有辅助)好像存在有辅助技巧(哔哩哔哩)1、游戏颠覆性...
透视积累!约局吧开挂(WePo... 透视积累!约局吧开挂(WePoKer安装)好像存在有辅助插件(哔哩哔哩)1、游戏颠覆性的策略玩法,独...