Hi Friends…i will show data from rest api, this is my code
search.html
<ion-header>
<ion-navbar color="secondary">
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Community</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-searchbar [(ngModel)]="inputSearch" [formControl]="searchControl" (ionInput)="onSearchInput()" [showCancelButton]="shouldShowCancel" ></ion-searchbar>
<button ion-button full (click)="getData()">CARI</button>
<div *ngIf="searching" class="spinner-container" text-center>
<ion-spinner>
</ion-spinner>
</div>
<ion-row *ngIf="!found">
<ion-col text-center>
<p>Data Not Found</p>
</ion-col>
</ion-row>
<ion-list>
<ion-item *ngFor="let hasil of result">
<h1>{{hasil.title}}</h1>
</ion-item>
</ion-list>
</ion-content>
search.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController, AlertController } from 'ionic-angular';
import { Budaya } from '../../classes/budaya.class';
import { FormControl } from '@angular/forms';
import { BudayaServices } from '../../services/budaya.service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@IonicPage()
@Component({
selector: 'page-search',
templateUrl: 'search.html',
})
export class SearchPage {
budayas: Budaya[] = [];
searching : any = false;
inputSearch : string = '';
searchControl : FormControl;
found: boolean = false;
data: Observable<any>;
txtCari:string;
result:any= [];
constructor(public navCtrl: NavController, public navParams: NavParams,private budayaService: BudayaServices,
private loadingCtrl: LoadingController, private alertCtrl: AlertController,public http: HttpClient) {
this.searchControl = new FormControl();
}
ionViewDidLoad() {
console.log('ionViewDidLoad SearchPage');
this
.searchControl
.valueChanges
.debounceTime(2000)
.subscribe(search => {
this.searching = false;
this.searchBudaya();
});
}
onSearchInput() {
this.searching = true;
}
searchBudaya(){
if (this.inputSearch.length > 0) {
let searchItem = this.inputSearch;
this.budayas = [];
let loader = this
.loadingCtrl
.create({content: 'Searching..'});
loader.present();
this.budayaService.searchByName(searchItem).subscribe(output=>{
loader.dismiss();
if(output.length>0){
this.found = true;
console.log(output);
this.budayas = output;
}else{
this.found = false;
}
},error=>{
loader.dismiss();
this.msgHandler('Error',error);
})
}else{
this.found = false;
}
this.searching = false;
}
msgHandler(title: string, message: string){
const alert = this
.alertCtrl
.create({title: title, message: message, buttons: ['Ok']});
alert.present();
}
getData(){
var url = 'http://localhost/mydb/api/search/rice/';
let loader = this.loadingCtrl.create({content: 'Please Wait! Data Request Processing ...'});
loader.present();
this.data = this.http.post(url, postdata);
this.data.subscribe(data => {
console.log(data);
this.result= data;
loader.dismiss();
//this.navCtrl.push(SearchPage, data)
//this.navCtrl.setRoot(SearchPage);
});
}
}
output rest api in console log
When I run it i found this error, how to solve it? Help Me Please
sorry my english not good, good luck in our live…Thanks