ionic-team / ionic-docs

https://ionicframework.com/docs
Apache License 2.0
589 stars 3.02k forks source link

How Can I call multiple methods from my constructor ? #633

Closed subhasishnath45 closed 5 years ago

subhasishnath45 commented 5 years ago

`import { Component } from '@angular/core'; import { NavController, NavParams, AlertController, Platform, ToastController } from 'ionic-angular'; import { ApiProvider } from '../../providers/api/api'; import { TruncatePipe } from '../../pipes/truncate/truncate'; import { KitchenDetailsPage } from '../kitchen-details/kitchen-details'; import { Network } from '@ionic-native/network'; import { errorHandler } from '@angular/platform-browser/src/browser';

@Component({ selector: 'page-home', templateUrl: 'home.html', // pipes: [TruncatePipe] }) export class HomePage { // following variable will hold single kitchen item in home page public kitchenitems:any = [];

public carouselItems:any = [];

// initially showing 5 latest posts, 5 posts at a time private per_page:number = 5; // initially shows page 1 private page:number = 1; private showLoadMore:boolean = false; private isLoading = false;

constructor(public navCtrl: NavController, public api:ApiProvider, public navParams:NavParams, private network:Network,public alertCtrl:AlertController,public platform:Platform, public toast:ToastController) { // watch network for a connection this.network.onConnect().subscribe(()=>{ this.toast.create({ message : 'Network connected.', duration: 3000 }).present(); });

this.network.onDisconnect().subscribe(()=>{
  this.toast.create({
    message : 'Network is disconnected.',
    duration: 3000
  }).present();
});

// console.log(this.navParams.get('cat_id'));
// following code is showing all kitchen posts by default in home page of the App.

Currently One of the following method is being called but when I try to call both of them, only the first one gets called.

// this.getKitchenPosts();
this.getHomeCarousels();

}

getKitchenPosts(){ if(!this.isLoading){ this.isLoading = true; // http://dlive.in/khobor/khonjkhobor/wp-json/wp/v2/Allkitchenposts-api?_embed&per_page=5&page=1 this.api.get('Allkitchenposts-api?_embed&per_page=' + this.per_page + '&page=' + this.page).subscribe((data:any) =>{ this.isLoading = false; this.kitchenitems = this.kitchenitems.concat(data); // data.length will tell us how many json objects are there in our json array. // this.per_page is 5 here // if No of json objects === 5 then don't show load more button. if(data.length === this.per_page){ this.page++; this.showLoadMore = true; }else{ this.showLoadMore = false; }

  },(error)=>{
    this.isLoading = false;
    if(error.error.code === "rest_post_invalid_page_number"){
      this.showLoadMore = false;
    }
  });
}

} getHomeCarousels(){ if(!this.isLoading){ this.isLoading = true; // http://dlive.in/khobor/khonjkhobor/wp-json/wp/v2/Allkitchenposts-api?_embed&per_page=5&page=1 this.api.get('homeCarousel-api?_embed&per_page=' + this.per_page + '&page=' + this.page).subscribe((data:any) =>{ this.isLoading = false; this.carouselItems = this.carouselItems.concat(data); // data.length will tell us how many json objects are there in our json array. // this.per_page is 5 here // if No of json objects === 5 then don't show load more button. if(data.length === this.per_page){ this.page++; this.showLoadMore = true; }else{ this.showLoadMore = false; }

  },(error)=>{
    this.isLoading = false;
    if(error.error.code === "rest_post_invalid_page_number"){
      this.showLoadMore = false;
    }
  });
}

} openKitchenDetails(kitchenitem){ // 1st parameter is page name // 2nd parameter is post of type kitchenitem this.navCtrl.push(KitchenDetailsPage,{post: kitchenitem}); }

getCatName(cat_id:number):string{ let cat_name:string = ''; this.api.Categories.forEach(element => { if(element.id == cat_id){ cat_name = element.name; } }); return cat_name; } ionViewDidLoad() {

}

} `

camwiegert commented 5 years ago

@subhasishnath45 I'm sorry, but I don't understand the issue you're having. Could you please create a minimal reproduction somewhere or clarify your question?

brandyscarney commented 5 years ago

Thanks for the issue! Please visit our forum or slack channel for questions about the framework. :smile:

If you have an idea for how we can improve the documentation, please open a new issue and fill out the provided template. Thanks!