我正在研究如何用打字稿过滤带Angular 2中数据的数组。过滤是针对Ionic 2中的搜索栏,模板中的列表正在获取数据并显示,但过滤不起作用。在角度2打字稿中过滤数组
initializeItems(i:number) {
this.http.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+this.placeid+'&key=AIzaSyBPi-ayuvkTht4wddCcvcy0l2wIBdT9P2w').map(res => res.json()).subscribe(data => {
this.locations[i]._detail = data.result;
this.names.push([
{
name: this.locations[i]._detail.name,
adress: this.locations[i]._detail.formatted_address,
phone: this.locations[i]._detail.formatted_phone_number
}
]);
this.items = this.names;
console.log(this.items);
});
}
getItems(ev) {
this.items = this.names;
console.log(this.items);
// set val to the value of the ev target
var val = ev.target.value;
// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
this.items = this.items.filter((item) => {
return (item.name.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
当我在搜索栏键入我得到这个错误。
用户被假定'item.name'指字符串。你对此有多确定? – Leon
item.name是一个字符串,但我不知道如何在过滤器中访问它? – Mohammed
你可以尝试用'let'声明'val'吗?即'let val = ev.target.value;' – AngularChef