Here's an example:
@Component({
selector: 'my-app',
template: `
<div>
<h1>{{ foo }}</h1>
<bpp [(foo)]="foo"></bpp>
</div>
`,
})
export class App {
foo;
}
@Component({
selector: 'bpp',
template: `
<div>
<h2>{{ foo }}</h2>
</div>
`,
})
export class Bpp {
@Input('foo') foo;
@Output('fooChange') fooChange = new EventEmitter();
ngAfterViewInit() {
const potentiallyButNotNecessarilyAsyncObservable = Observable.of(null);
potentiallyButNotNecessarilyAsyncObservable.subscribe(() => {
this.fooChange.emit('foo');
})
}
}
where error occasionally appears:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'foo'
It results from the fact that two-way binding is updated by an observable that can get a value on same tick. I would prefer to not wrap the logic above with setTimeout
because it looks like a hack and complicates control flow
What can be done to avoid this error here?
Does ExpressionChangedAfterItHasBeenCheckedError
error have ill effects or can it be ignored? If it can, can change detector be silent on it and not pollute the console?