Javascript 使用管道筛选数据时UI未更新
HTML:Javascript 使用管道筛选数据时UI未更新,javascript,angular,Javascript,Angular,HTML: 函数performSearch返回过滤结果,但管道不返回 因此,您还必须返回结果 (3) [{…}, {…}, {…}] 0: {title: "LittleBigPlanet PS Vita", platform: "PlayStation Vita", score: 9, genre: "Platformer", editors_choice: "Y"} 1: {title: "Little
函数performSearch返回过滤结果,但管道不返回 因此,您还必须返回结果
(3) [{…}, {…}, {…}]
0: {title: "LittleBigPlanet PS Vita", platform: "PlayStation Vita", score: 9, genre: "Platformer", editors_choice: "Y"}
1: {title: "LittleBigPlanet PS Vita -- Marvel Super Hero Edition", platform: "PlayStation Vita", score: 9, genre: "Platformer", editors_choice: "Y"}
2: {title: "New Little King's Story", platform: "PlayStation Vita", score: 5.8, genre: "RPG", editors_choice: "N"}
length: 3
__proto__: Array(0)
你的烟斗不回任何东西。内部的
performSearch
函数看起来像是在执行搜索,但将其包装在setTimeout()
中会导致管道返回未定义
不清楚为什么要使用setTimeout()
,因此我建议将其删除
timeout = setTimeout(() => {
return performSearch(searchText);
}, 2000);
transform(items:any,searchText:string):any[]{
如果(!项){
返回[];
}
如果(!searchText){
退货项目;
}
const txt=searchText.toLowerCase();
如果(txt.length<5){return;}
const filtered=items.filter((e:any,i:number)=>{
返回e.title.toLowerCase().includes(txt);
});
console.log('filtered',filtered);
返回过滤;
}
但我不明白为什么它不带timeout?setTimeout()只是在指定的毫秒数后执行一个函数。你的管道不会等待它执行。也许我可以试试承诺,如果你有异步转换,我想看看Angular async管道
(3) [{…}, {…}, {…}]
0: {title: "LittleBigPlanet PS Vita", platform: "PlayStation Vita", score: 9, genre: "Platformer", editors_choice: "Y"}
1: {title: "LittleBigPlanet PS Vita -- Marvel Super Hero Edition", platform: "PlayStation Vita", score: 9, genre: "Platformer", editors_choice: "Y"}
2: {title: "New Little King's Story", platform: "PlayStation Vita", score: 5.8, genre: "RPG", editors_choice: "N"}
length: 3
__proto__: Array(0)
timeout = setTimeout(() => {
return performSearch(searchText);
}, 2000);
transform(items: any, searchText: string): any[] {
if (!items) {
return [];
}
if (!searchText) {
return items;
}
const txt = searchText.toLowerCase();
if (txt.length < 5) { return; }
const filtered = items.filter((e: any, i: number) => {
return e.title.toLowerCase().includes(txt);
});
console.log('filtered ', filtered);
return filtered;
}