簡體   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