在rxjs中,我首先尝试获取所有数据,然后我需要继续下一行

问题描述 投票:0回答:1

在Angular 5中,我有可观察的数组服务,我在组件中订阅它。在下一行声明中,我正在检查数据是否已填充。但我发现变量尚未填充。

https://github.com/vsaravanan/ngx-mat-select-search.git

ERROR TypeError:无法读取未定义的属性'slice'错误@ this.filteredBanksMulti.next(this.banks.slice());

我有一个从jsonserver @ port 3000运行的示例数据

        "banks"  :
        [
            { "name" : "apphugIndia", "id": "A"},
            { "name" : "batHugFr", "id": "B"},
            { "name" : "Bank C (France)", "id": "C"},
            { "name" : "hungary", "id": "D"},
            { "name" : "Hungary", "id": "E"},
            { "name" : "Bank F (Italy)", "id": "F"}
        ]

如何解决这个问题?

json angular rxjs observable fetchall
1个回答
1
投票

如果你这样做

this.httpCall.getData().subscribe(data=>this.recoreds = data);
this.records 

那么它不会起作用,你调用服务器仍然不完整你必须等待数据填充,这意味着你只能在subscribe方法中进行操作。

为避免出现问题,您需要将代码放入订阅中

this.httpCall.getData().subscribe(data=> {
      this.records = data;
      //do code here which uses  this.records
 });
© www.soinside.com 2019 - 2024. All rights reserved.