I am facing a situation where I have a variable outside of a function that needs to be updated, but I am unable to access it using "this" as it is not reachable at that point in the code.
export class GamesDetailPage {
details : any = {};
type : String;
level : Number;
cards : any = {}; // This is the variable I need to change
constructor(public navCtrl: NavController, public http: HttpClient , private device: Device, private serviceProvider: ServicesProvider,
public navParams: NavParams
) {
this.type = navParams.get('gameType');
this.level = navParams.get('gameLevel');
}
ionViewDidLoad() {
this.getCards(); // Invoking the method here
}
getCards(){
var deviceUUID = this.device.uuid;
var platform = this.device.platform;
var cardsReq = {"gameType": this.type ,"gameLevel": this.level};
var dialog = this.dialogs;
this.serviceProvider.getCards(deviceUUID, platform, cardsReq)
.then(function (res){
this.cards = res;// Need to set the variable value here, but "this" is undefined
})
.catch(function(err){
console.log("Error");
})
}
}