2种方式绑定ngModel的更改也反映在其分配的temp变量中(在页面加载时分配)

时间:2019-05-21 06:27:57

标签: angular angular-ngmodel 2-way-object-databinding ngmodel

我有2种方式的绑定模型,我在页面加载时将其分配给temp变量。但是temp变量也会改变NgModel。

this.questionService.getAll(this.id, this.bcID).subscribe(_result => {
      this.questions = _result.data.filter(x => x.isDeleted === false);
      this.tempQuestions = _result.data.filter(x => x.isDeleted === false);
    });

使用KEndo UI网格

<kendo-grid [data]="gridView"
              [height]="550"
              [skip]="gridState.skip"
              (edit)="editHandler($event)"">
this.gridView = process(this.questions, this.gridState);

1 个答案:

答案 0 :(得分:0)

基本上,变量this.questionsthis.tempQuestions都指向相同的对象。为确保更改不会反映到this.tempQuestions,请对this.questions对象进行深层克隆。

这会有所帮助

this.questionService.getAll(this.id, this.bcID).subscribe(_result => {
  this.questions = _result.data.filter(x => x.isDeleted === false);
  this.tempQuestions = JSON.parse(JSON.stringify(this.questions));
});