【发布时间】:2017-09-13 14:40:06
【问题描述】:
我在我的 Angular 应用程序中使用 observables,它按预期工作。但是,我遇到了一种有点令人困惑的情况。在一个组件中,我成功订阅了 observable,然后过滤结果,如下所示:
this.clientsService.getAll()
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.inactiveRecords = this.records.filter(record => record.category && record.category.includes('inactive'));
this.records = this.inactiveRecords;
},
responseRecordsError => this.errorMsg = responseRecordsError);
但是,在另一个组件中,虽然我在做几乎相同的事情,但我在控制台上打印了一个错误,内容如下:
例外:_this.records.filter 不是函数
这就是那个组件的样子:
optionReceived(option) {
console.log('Consulting: ' + option.toolbarLabel);
if (option.toolbarLabel === 'OT') {
console.log('Consulting: OT Filter Activated!');
this.clientService.getByCategory('consulting', 1, this.pagesize)
.subscribe(resRecordsData => {
this.records = resRecordsData;
this.OTRecords = this.records.filter(record => record.type && record.type.includes('OT'));
this.records = this.OTRecords;
},
responseRecordsError => this.errorMsg = responseRecordsError);
} else {
return this.records;
}
第二种情况的问题是什么?为什么我在那里得到错误,而不是在第一种情况下?
【问题讨论】:
-
如果您在每种情况下都将鼠标悬停在 this.records 上,您会看到数据类型吗?这两个例子有什么不同吗?
-
它们看起来一样:(属性)ConsultingComponent.records: any[]
-
"records" 在两个组件中都设置为空数组。
标签: javascript arrays angular filter