How to make json response available within the same ts file


#1

Hi

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, AlertController } from 'ionic-angular';
import { AnimeService } from '../../app/services/anime.service';

@IonicPage()
@Component({
  selector: 'page-anime-by-season',
  templateUrl: 'anime-by-season.html',
})
export class AnimeBySeasonPage {

  animes: any;
  seasons: any;
  testRadioOpen: boolean;
  statusRadioResult;

  constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController, private animeService:AnimeService) {
    console.log('constructor');
    this.getSeasons();
  }

  ngOnInit(){
    console.log('ngOnInit');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad');

    let someArray = this.seasons;

    for (let entry of someArray) {
        console.log(entry);
    }

  }



  getSeasons(){
    console.log('getSeasons');
      this.animeService.getSeasons().subscribe(response => {
          this.seasons = response;
          console.log("Success : "+this.seasons);
      });
  }

}

this is my code. My problem is that the variable seasons has undefined value even when the method getSeasons is invoked… I would like to access data stored in seasons vairable within the same .ts file… I can access it inside html file but in ts file I cannot.

Hope you can help me
Thank you


#2

Just use this directly in ionViewDidLoad instead of your own method. Only then you can use the async stuff there and only work with the data after you actually got it.


#3

Thank you very much!
That’s solve my problem