I had a very similar problem, and this was due to how the Angular2 detection function works. There are several solutions that worked for me.
Save the link to the observable so that it does not change
Right now, when you call personsgetter, it always returns a new instance Observable(for example, another object), so Angular reevaluates all of this, and during the next change detection cycle, it does it again and then again ... So, here's how this can be solved:
@Component({
selector: 'my-comp',
template: `{{ _persons | async }}`,
}) export class MyComponent implements OnInit {
ngOnInit() {
this._persons = this.personService.list();
}
}
Change ChangeDetectionStrategytoOnPush
You can tell Angular: "I know what I'm doing, Iβll tell you myself when some changes occurred":
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'my-comp',
template: `
{{ persons | async }}
<button (click)="reload">Reload</button>
`,
}) export class MyComponent implements OnInit {
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() {
this.persons = this.personService.list();
}
reload() {
this.persons = this.personService.list();
this.cd.markForCheck();
}
}
, ,