10

我有一个角度材料选择组件,我想将下拉列表的选定值绑定到来自 firebase 的标量 Observable。我想在不打开组件中的 observable 的情况下做到这一点。看起来我无法使用异步管道绑定值。下面的代码会抛出异常,因为 mat-select 的值不能绑定到“uiStateSvc.currentClient$ | async”。

<mat-form-field *ngIf="(store.authorizedClients$ | async)?.length > 0">
  <mat-select [(value)]="uiStateSvc.currentClient$ | async" [compareWith]="compareFn">
    <mat-option *ngFor="let client of store.authorizedClients$ | async" [value]="client"
    (onSelectionChange)="changeCurrentClient($event, client)">
      {{ client.name}}
    </mat-option>
  </mat-select>
</mat-form-field>

我正在从 firebase 中提取下拉列表的当前选定值,如下所示:

this.selectedClient$ = this.authSvc.currentUser$.pipe(
      switchMap((x: firebase.User) => {
        if (x != null) {
          return this.afs.doc(`uistate/${x.uid}`).valueChanges().map((y: any) => y.selectedclient);
        } else {
          return Observable.of(null);
        }
      })
    );
4

1 回答 1

5

你正在使用双重绑定,你不能用管道做到这async一点它只适用于组件中的一个变量,下面的代码应该可以工作,注意我将 [(value)] 更改为 [value] 所以它会从 observable 中读取| async选择的默认值,并将我看到的更改存储在 mat-option(onSelectionChange)="changeCurrentClient($event, client)中,这应该足够了

<mat-form-field *ngIf="(store.authorizedClients$ | async)?.length > 0">
  <mat-select
    [value]="uiStateSvc.currentClient$ | async"
    [compareWith]="compareFn"
  >
    <mat-option
      *ngFor="let client of store.authorizedClients$ | async"
      [value]="client"
      (onSelectionChange)="changeCurrentClient($event, client)"
    >
      {{client.name}}
    </mat-option>
  </mat-select>
</mat-form-field>

希望能帮助到你

于 2018-06-05T20:50:09.527 回答