I am making http call to fetch the data from the local json file. Due to lateness in receive the response, I am not able to display the response data on the template.
Service is making a HTTP call to fetch the data from json file.
export class DataBook {
bookDetails: Array<Object>;
constructor(public http: Http) {
this.bookDetails = [];
}
makeDataReady(){
this.http.get('assets/preloaded-data/booksummaries.json')
.map(res => res.json())
.subscribe(
data => this.bookDetails = data,
error => alert(error),
() => console.log(this.bookDetails) //output the json file content
);
}
appendBook(book:Object)
{
this.bookDetails.push(book);
}
getBooks():Array<Object>
{
return this.bookDetails;
}
}
The below component is making call to initiate the http call.
export class ShowreviewPage {
items: Object;
ngOnInit(){
this.items = this.dataBook.makeDataReady();
}
constructor(public navCtrl: NavController, public navParams: NavParams, public dataBook: DataBook) {
this.items = this.dataBook.getBooks();
}
ionViewDidLoad() {
console.log("ion View did Load",this.items); // empty array output
}
}
I am new to Angular2. I am struggling to understand why there is a delay in the response?