I have an observable that fetches information from the backend which I then use to fill values in a form like this:
<div class="column">
<div class="row">
<mat-select
name="selectField"
[options]="someOptions"
[value]="(store.observable$ | async).selectFieldValue"
[required]="true"
>
</mat-select>
<mat-input
name="inputField"
[required]="true"
[value]="(store.observable$ | async).inputfieldValue"
>
</mat-input>
<mat-input
name="otherInputField"
[required]="true"
[value]="(store.observable$ | async).otherInputFieldValue"
>
</mat-input>
...
</div>
</div>
The observable comes from a selector in my component's ComponentStore:
export class MyComponentStore extends ComponentStore<MyStoreClass> {
constructor(
private service: MyHttpService,
) {
super({ ... });
}
readonly observable$ = this.select((state) => state.storeClass);
readonly updateObservable = this.updater(
(state, payload: SomeClass) => {
return {
...state,
observable: payload
};
}
);
getObservableInfo = this.effect((req$: Observable<void>) => {
return req$.pipe(
switchMap((_) => this.service.getInfo()),
map((payload) => this.updateObservable(payload.data))
);
})
}
I then call this.store.getObservableInfo()
in my component's ngOnInit
. I was getting an ExpressionChangedAfterItHasBeenCheckedError error if I tried to do this:
<div class="column" *ngIf="(store.observable$ | async); let observable">
<div class="row">
<mat-select
name="selectField"
[options]="someOptions"
[value]="observable.selectFieldValue"
[required]="true"
>
...
So I dismissed that idea. But I'm bothered by having to write (store.observable$ | async)
in each of my controls' values. Is there a way to declare that as a variable somewhere and simplify it like you can do with *ngIf without using *ngIf (to avoid the error)?
Thank you