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

In rxjs, I try to fetch all the data first then I need to proceed to next line

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

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

错误类型错误:无法读取未定义的 属性 '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"}
        ]

如何解决这个问题?

如果你这样做

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
 });