【问题标题】:How to replace an observable array by array from API response?如何从 API 响应中用数组替换可观察数组?
【发布时间】:2020-10-30 15:06:33
【问题描述】:

我正在为我的动态表单组件中的选择项使用自定义材料自动完成输入。正如我所说,表单字段是动态的,要过滤项目列表,每次用户在输入中输入内容时,我都必须更改列表。

filteredOptions: { [key: string]: Observable<PickListItem[]> } = {}; key 是 controlName 的名称, PickListItem 是要选择的项目列表。

在某些情况下,用户可以将自己的选项添加到列表中,之后有 API 请求返回更新的项目列表,这是我的问题:我想用新列表替换旧列表(更新)。

有人可以帮我吗?

this.componentsApiService.addPicklistItem(field.Attributes.List.ID, this.addInputOption)
      .pipe(
        finalize(() => {
          this.store.dispatch(new StopLoading('shared.saving'));
        }),
        takeUntil(this.componentDestroyed$))
      .subscribe( result /*updated list*/ => {
        this.filteredOptions[field.Label] = <= here I want to assign observable updated list, dont know how to do this
        this.addInputOption = '';
      }, ({error}) => {
        this.snackBarService.error(error.message);
      });

【问题讨论】:

    标签: javascript arrays angular rxjs subscription


    【解决方案1】:

    你可以使用 rxjs of operator 来创建一个 observable

    import { of } from 'rxjs';
    
    this.filteredOptions[field.Label] = of(result);
    

    【讨论】:

    • 或者,您可以使用from 运算符。
    猜你喜欢
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2019-08-10
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多