My underlying problem is I need to set an asynchronous function that alters an attribute of the host. Since @HostBinding has no asynchronous abilities, I set a instance variable on the component, bound that, and manually maintained that variable. To get it to work without the infamous ExpressionChangedAfterItHasBeenCheckedError, I needed to do this
@Component(...)
export class MyComponent implements OnChanges {
constructor(private readonly cdr: ChangeDetectorRef) {}
@Input() inputObservable: Observable<boolean>;
@HostBinding('class.some-class') private setCssClass: boolean;
ngOnChanges() {
this.inputObservable.do(v => {
if (this.setCssClass !== v) {
setTimeout(() => {
this.setCssClass = v;
this.cdr.detectChanges();
}, 0);
}
})
.subscribe();
}
}
which is just awful.
Is there some cleaner way to tell the parent "here's a new value for one of your variables, set it at your leisure"?
EDIT:
And it doesn't even work. That setTimeout can, under special circumstances, execute after the component has officially been destroyed, which causing an exception needs an whole 'nother level of hacky horribleness to prevent.