角4个rxjs:

角4个rxjs:

问题描述:

我有可观察到的一个阵列的可观察到的阵列筛选数据:角4个rxjs:

schools: Observable<SchoolVM[]>; 

我需要此可观察到过滤到另一个用于自动填充选择

filteredSchools: Observable<SchoolVM[]>; 

予订阅form.valueChanges

this.formChange = this.searchForm.valueChanges 
    .debounceTime(300) 
    .subscribe(value => { 
    this.filteredSchools = this.schools.filter(s => s.SchoolName == value); 
    }); 

我有一个错误:属性'SchoolName'不存在类型'Sch oolVM []' 我该如何解决它? 还有:我必须过滤2个不同的标准。这是正确的过滤像

this.filteredSchools = this.schools.filter(s => s.SchoolName == value || 
    s => s.SchoolName == value); 

我改写了以下Aakash Jain's answer,这样的功能:

this.formChange = this.searchForm.valueChanges 
     .debounceTime(300) 
     .subscribe(value => { 
     this.filteredSchools = this.schools.map(schools => { 
      return schools.filter((school: SchoolVM) => (school.SchoolName === value) || (school.City === value)); 
      // this.logger.info(this.filteredSchools); 
     }); 
     }); 

,现在我得到的错误“schools.filter”不是一个函数。

可观察

schools: Observable<SchoolVM[]>; 

是从HttpClient的通话填补:

this.schoolService.filterSchools(arg) 
     .subscribe(d => {this.schoolsToFilter = d; }) 

而且在服务:

filterSchools(arg: string): Observable<any> { 
    return Observable.from(this.http.get('/api/school/find/' + arg)); 

也许他们是可观察到的一个问题,但学校可观察到的很好填充:

{ “SchoolID”:13028, “SchoolName”: “100%Conduite”, “城市”: “MEUDON”, “邮编”: “92190”, “街”: “17街德加仑” , “手机”: “01 49 66 06 02” }, { “SchoolID”:6803, “SchoolName”: “100%PERMIS”, “城市”: “巴东维莱”, “邮编”: “54540”, “街”: “25街报马雷夏尔福煦”, “电话”: “” },...

schoolsfilteredSchools是每个包含SchoolVM对象的数组的观察对象。

所以,当你这样做:

this.schools.subscribe(s => console.log(s)); 

你会发现,SchoolVM一个数组被打印出来。所以上面的s的值不是一个单独的SchoolVM而是一个SchoolVM[]

因此,当你写:

this.filteredSchools = this.schools.filter(s => s.SchoolName == value); 
// this will not work since s is an array 

相反,你想要做的是:

this.filteredSchools = this.schools.map(schools => { 
    return schools.filter((school: SchoolVM) => school.SchoolName === value); 
}); 

要通过两个条件进行过滤,用自己的方式不正确。你正在做的是定义两个匿名函数。这会给你一个语法错误。相反,你需要一个具有OR条件的匿名函数。

this.filteredSchools = this.schools.map(schools => { 
    return schools.filter((school: SchoolVM) => (school.SchoolName === value) || (school.NumberOfStudents >= 100)); 
}); 
+0

我改写这样的自动选择的代码: this.formChange = this.searchForm.valueChanges .debounceTime(300) .subscribe(值=> { this.filteredSchools = this.schools.map(学校((School:SchoolVM)=>(school.SchoolName === value)||(school.City === value)); // this.logger.info(this.filteredSchools ); }); });现在我得到错误“schools.filter不是一个函数”。请帮忙吗? – Marcos

+0

@Marcos尝试'this.formChange = this.searchForm.valueChanges.debounceTime(300).subscribe(val ue => this.schools.subscribe(schools => console.log(schools)))'让我知道什么它记录。 –

+0

非常感谢您的帮助Aakash。与您的函数我有错误错误TypeError:_this.schools.subscribe不是一个函数。也许是因为学校是可观察的? – Marcos