Zaza
Zaza

Reputation: 406

How to handle the data from server and binding values dynamically in ionic 3?

I have so confused when handling the data from the server. I have a provider that fetch the data from the server through HTTP. I want to load the data before the page can be loaded. but the data will take a time to reflect on the page. Anyone please guide me to handling data from server

here is my code

workpage.ts

 loadWork(){
    this.work.loadwrk().then(hw=>{
      this.work = work ;
    })
  ionViewWillEnter(){
     this.loadWork();
    }

workprovider.ts

loadwrk():Promise<any>{
    return new Promise(resolve =>{
      this.storage.get('works').then(ow=>{
      this.http.get(---url----).subscribe((res)=>{ 
        let resData = res.json();
        if(JSON.stringify(ow) !=JSON.stringify(resData)){  
         console.log('i am new work');           
          resolve(resData);             
      }
      else{  
      resolve(ow); 
        console.log('i am old work');
      }

      })    
    })
  })
  }

Upvotes: 0

Views: 312

Answers (2)

ELTEYAB
ELTEYAB

Reputation: 19

you can use ionic loader or ngIf show value if fetch data is done

    <h1 *ngIf="item.Response !== null">#: {{item.Response.id}}</h1>

.

 loadWork(){
this.work.loadwrk().then(hw=>{
  this.work = work ;
});
}

 ngAfterViewInit() {
  this.loadWork();}

and i see error in call function in this code

 loadWork(){
this.work.loadwrk().then(hw=>{
  this.work = work ;
})


 ionViewWillEnter(){
     this.loadWork();
    }

is most be like this

 loadWork(){
this.work.loadwrk().then(hw=>{
  this.work = work ;
});
}

 ionViewWillEnter(){
     this.loadWork();
    }

or you can make page show first and fetch the data and resend in NavParams this issues in ionic wen try view native or Map or ChartJs

Upvotes: 1

Chandru
Chandru

Reputation: 11182

Try like this :

loadwrk(): Promise<any> {
    return new Promise<any>((resolve, reject) => {
        this.storage.get('works').then(ow => {
            this.http.get(---url----).subscribe((res) => {
                let resonseData = res.json();
                if (JSON.stringify(ow) != JSON.stringify(resonseData)) {
                    console.log('i am new work');
                    resolve(resonseData);
                }
                else {
                    resolve(resonseData);
                    console.log('i am old work');
                }

            })
        })
    })
}

Upvotes: 1

Related Questions