我很新的Typescript和離子2,我試圖過濾槽與Ionic 2搜索欄的JSON響應。搜索欄與過濾器和來自JSON數據與離子2
這是我的代碼:
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
posts: any;
private searchQuery: string = '';
private items: string[];
constructor(private http: Http) {
this.initializeItems();
this.http.get('https://domain.co/open.jsonp').map(res => res.json()).subscribe(data => {
this.posts = data;
console.log(this.posts);
});
}
initializeItems() {
this.items = this.posts;
}
getItems(ev: any) {
// Reset items back to all of the items
this.initializeItems();
// set val to the value of the searchbar
let 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.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
}
而且標記:
<ion-header>
<ion-searchbar (ionInput)="getItems($event)" [debounce]="500" placeholder="Suchen..."></ion-searchbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let post of posts">
<h1>{{post.storeName}}</h1>
</ion-item>
</ion-list>
</ion-content>
我這個錯誤,當我搜索:
item.toLowerCase不是一個函數
JSON數據是這樣的:
[
{
storeName: "Avec Hauptbahnhof",
addressLink: "",
phone: "0326223902",
image: "",
description: "",
link: "",
openingHours: [
"05.30 - 22:00",
"05.30 - 22:00",
"05.30 - 22:00",
"05.30 - 22:00",
"05.30 - 22:00",
"06.30 - 22:00",
"7.00 - 22.00"
]
},
{
storeName: "Manor",
addressLink: "",
phone: "0326258699",
image: "",
customer: "",
description: "",
link: "",
openingHours: [
"09.00 - 18.30",
"09.00 - 18.30",
"09.00 - 18.30",
"09.00 - 21:00",
"09.00 - 18.30",
"08.00 - 17.00",
"Geschlossen"
]
}
]
'https:// domain.co/open.jsonp'返回字符串列表嗎? – sebaferreras
它返回一個json對象 – olivier
你可以在OP中添加那個json對象的樣子嗎? – sebaferreras