我得到这个错误:"friend.toLowerCase“不是一个函数。应用ionic的searc函数时出错。在我的程序中唯一不同的是,我不是只有一个JSON项目的列表,而是一个每个项目有5个项目的列表,比如friend.name,friend.status,e.t.c。我改变了一些东西,比如离子列表被称为items,我的friendsList。我认为这个错误可能是由混淆的名字引起的。但是我没有发现错误。
HTML
<ion-searchbar (ionInput)="getFriends($event)"</ion-searchbar>
...
<ion-item *ngFor="let friend of friendsList">
...
<h2>{{friend.name}}</h2>
...TS
export class RankfriendsPage {
friendsList;
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.initializefriendsList();
}
initializefriendsList() {
this.friendsList = [
{
"name": "Alice", //list example
"status": "Online",
"img": "img/6.jpeg",
"img2": "img/33.jpeg",
"text": "+ADD"
},
];
}
getFriends(ev) {
// Reset items back to all of the items
this.initializefriendsList();
// 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.friendsList = this.friendsList.filter((friend) => {
return (friend.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}发布于 2018-07-30 18:14:06
问题是因为没有带有toLowerCase()函数的朋友对象,我猜您需要,
this.friendsList = this.friendsList.filter((friend) => {
return (friend.name.toLowerCase().indexOf(val.toLowerCase()) > -1);
})发布于 2018-07-30 18:05:48
friend = {
"name": "Alice",
"status": "Online",
"img": "img/6.jpeg",
"img2": "img/33.jpeg",
"text": "+ADD"
}正如您所看到的,那里没有toLowerCase属性。
如果您希望使用它,可以在字符串上使用它,例如friend.name。
发布于 2018-07-30 18:09:11
Friend是一个对象,并且对象没有toLowerCase()属性。
只能将toLowerCase()应用于字符串。
您可以使用friend.status.toLowerCase()、friend.name.toLowerCase()等。
https://stackoverflow.com/questions/51591537
复制相似问题