Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

angular - {{myArray}} now updates in the view as of beta.16

Change detection has changed.

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.

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.

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?

Plunker code:

@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);
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 stack

enter image description here

enter image description here

As you can see there is a comparison of arrays

RC.1 stack

enter image description here

enter image description here

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

May it will help you :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...