本文介绍了{{myArray}}现在从beta 16开始在视图中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改检测已更改.

在beta.16之前,如果您的视图包含{{myArray}},则如果您不修改数组引用,则该绑定不会更新.例如,如果您将push()项添加到数组中,则视图将不会更新以显示新项.解释是(因为是)因为数组引用未更改,所以角度更改检测不会重新评估绑定.此 beta.15窃听器演示了此行为.

Before beta.16, if your view contains {{myArray}}, that binding won't update if you don't modify the array reference. E.g., if you push() items onto the array, the view won't update to show the new item. The explanation is (well, was) that because the array reference didn't changed, Angular change detection doesn't reevaluate the binding. This beta.15 plunker demonstrates this behavior.

从beta.16(因此是RC.1)开始,情况有所不同.现在,即使数组引用未更改,{{myArray}}绑定也将更新!参见此 RC.1塞子.

As of beta.16 (and hence RC.1), things are different. The {{myArray}} binding will now update even if the array reference hasn't changed! See this RC.1 plunker.

我查看了 ChangeLog for beta.16 ,而且我看不到任何可以解释这种行为变化的东西(但也许我错过了一些东西).有谁知道是什么导致了这一变化,还有什么可能受到影响?

I looked at the ChangeLog for beta.16, and I don't see anything that would account for this change in behavior (but maybe I missed something). Does anyone know what caused this change, and what else might be affected?

柱塞代码:

@Component({
  selector: 'child',
  template: `<p>child: {{arr}}`
})
export class Child {
  @Input() arr;
}
@Component({
  selector: 'my-app',
  template: `{{title}} <p>parent: {{arr}}
    <button (click)="modifyArray()">modify array</button>
    <child [arr]="arr"></child>`,
  directives: [Child]
})
export class AppComponent {
  title = "Angular 2 beta.15";  // or "Angular 2 RC.1", as appropriate
  arr = 'one two three'.split(' ');
  modifyArray() {
    this.arr.push('another');
    console.log(this.arr);
  }
}

推荐答案

我认为与DetectChanges相关的代码已更改( ChangeDetector.detectChangesInRecordsInternal beta.15与 View.detectChangesInternal > rc.1).您可以在图片中看到它.

I think that code related with DetectChanges is changed (ChangeDetector.detectChangesInRecordsInternal beta.15 vs View.detectChangesInternal rc.1). You can see it in pictures.

Beta.15堆栈

如您所见,有一个数组比较

As you can see there is a comparison of arrays

RC.1堆栈

然后我们可以看到表达式(字符串)的比较,它们是不同的.因此,角度rc.1将更新视图.

Then we can see comparison of expression(string) and they're different. So angular rc.1 will update view.

愿它对您有帮助:)

这篇关于{{myArray}}现在从beta 16开始在视图中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:35