繁体   English   中英

angular2 @Input EventEmitter 更新视图

[英]angular2 @Input EventEmitter update view

我正在尝试制作一个简单的网格组件,但在发出事件后更新视图时遇到了麻烦! 请谁知道,为什么更新简单组件后,视图不重新渲染? 这段代码有什么问题?

export class GridComponent implements OnInit {
  @Input() resource: any [];
  @Output() saveModel = new EventEmitter();
  @Output() deleteModel = new EventEmitter();
  attributes: any[];
  isUpdating: boolean = false;
  updatingID: number;

  constructor() {}

  ngOnInit() {
    this.attributes = Object.keys(this.resource[0]);
  }

  toggleUpdate(id, flag = true) {
    this.isUpdating = !this.isUpdating;
    this.updatingID = flag ? id : undefined;
  }

  destroy(id) {
    this.deleteModel.emit(id);
  }

  save(model) {
    this.saveModel.emit(model);
    this.toggleUpdate(model.id, false);
  }

  cancel(id) {
    this.toggleUpdate(id, false);
  }

}

完整示例在这里https://plnkr.co/edit/InxsHu9GwCtMplYoocsS?p=preview

resource数据在父组件和子组件中正确更新,只是表单不显示更新。

我认为您需要更改values管道以仅返回键而不返回值,然后使用带有键的*ngFor变量访问值以直接获取视图中的值。

编辑: Günter Zöchbauer,谢谢你节省了我的时间!

我认为您需要更改值管道以仅返回键而不返回值,然后使用带有键的 *ngFor 变量访问值以直接获取视图中的值。

这是邪恶的根源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM