首页 > 解决方案 > 添加可观察的可观察的

问题描述

我正在使用 myObject 数组的 observable 来填充我的 html 模板中的输入类型选择。我希望能够根据条件向我的列表中添加另一个选项。

=> 我想添加myItem = Observable <myObject>到我的myList = Observable<myObject[]>

我从 http 方法中检索两个 observables。我在文档中找不到任何操作员来执行此操作。

我可以通过像这样订阅 myItem 来做到这一点

myList.pipe(
 map(x=> myItem.subscribe(y=> x.push(y)))
)

但我更愿意在没有订阅的情况下这样做。

我的模板就像

<mat-form-field>
  <mat-select [value]="selected$ | async" (selectionChange)="getSelectedValue($event)">
    <mat-option *ngFor="let item of myList | async" [value]="item.Id">{{item.Name}} </mat-option>
  </mat-select>
</mat-form-field>```

标签: angulartypescriptrxjsangular7

解决方案


您可以使用Observable.concat

myList = Observable.concat(myList, from(myItem));

https://www.learnrxjs.io/operators/combination/concat.html


推荐阅读