Javascript 我无法返回值(RXJS)
我想返回一个值,但我不能。。 我不知道该怎么办 接收到的值:Javascript 我无法返回值(RXJS),javascript,typescript,rxjs,Javascript,Typescript,Rxjs,我想返回一个值,但我不能。。 我不知道该怎么办 接收到的值: public filterOptions: Observable<string[]>; 公共过滤器选项:可观察; 我要返回的代码: this.filterOptions = this.myForm.get('search').valueChanges.pipe( startWith(null), map((val) => { if (!val) {
public filterOptions: Observable<string[]>;
公共过滤器选项:可观察;
我要返回的代码:
this.filterOptions = this.myForm.get('search').valueChanges.pipe(
startWith(null),
map((val) => {
if (!val) {
this.suscriptionSearch.unsubscribe();
return null;
}
if (this.bool) {
const filterValue = val.trim().toLowerCase();
const obj = {search: ''}
obj.search = filterValue;
this.suscriptionSearch = this.userService.searchUser(obj).subscribe((data)=>{
let monTab:string[] = [];
for (let index = 0; index < data.length; index++) {
monTab[index] = data[index].nom
}
return monTab.filter((option) => {
return option.toLowerCase().startsWith(filterValue)
}) //the value to return
});
}
}));
this.filterOptions=this.myForm.get('search').valueChanges.pipe(
startWith(空),
地图((val)=>{
如果(!val){
此.suscriptionSearch.unsubscribe();
返回null;
}
如果(this.bool){
常量filterValue=val.trim().toLowerCase();
常量obj={search:'}
obj.search=filterValue;
this.suscriptionSearch=this.userService.searchUser(obj.subscribe)((数据)=>{
让monTab:string[]=[];
for(让index=0;index{
return option.toLowerCase().startsWith(filterValue)
})//要返回的值
});
}
}));
我找到了解决方案。。我和你分享
this.filterOptions = this.myForm.get('search').valueChanges.pipe(
startWith(null),
switchMap((val) => {
if (this.bool) {
const filterValue = val.trim().toLowerCase();
const obj = {search: ''}
obj.search = filterValue;
return this.userService.searchUser(obj).pipe(map((data)=>{
let monTab:string[] = [];
for (let index = 0; index < data.length; index++) {
monTab[index] = data[index].nom
}
console.log(monTab);
return monTab.filter((option) => {
return option.toLowerCase().startsWith(filterValue)
});
}))
} else {
return of(null);
}
}));
this.filterOptions=this.myForm.get('search').valueChanges.pipe(
startWith(空),
开关映射((val)=>{
如果(this.bool){
常量filterValue=val.trim().toLowerCase();
常量obj={search:'}
obj.search=filterValue;
返回此.userService.searchUser(obj).pipe(映射((数据)=>{
让monTab:string[]=[];
for(让index=0;index{
return option.toLowerCase().startsWith(filterValue)
});
}))
}否则{
返回(空);
}
}));
看起来您并没有返回可观察到的。你也不想用一个新的来覆盖可观测的。如果你想拥有option.toLowerCase()…
你可能需要使用一个过滤器,我实际上认为这很奇怪,你为服务提供了一个过滤器,然后又在收到的列表上使用了同样的过滤器值。好的,服务可能使用包含,然后您检查StartWith,但是让服务得到增强,这样您就只接收带有结果的StartWith不是更好吗?