My first attempt with Promise feature of angular. I'm trying to assign the value in the promise to a variable but i cannot get it to work. I want to use this final variable to populate the country data in 'select' dropdown.
console.log("1",this.globals.countryMasterDummy);
this.globals.countryMasterDummy.then(function(data){console.log("2",data)});
this.globals.countryMasterDummy.then(function(data: Countrymaster){ this.scanCountries = data});
console.log('3', this.scanCountries) ;
this.globals.countryMasterDummy.then((data) => {
this.scanCountries = data;})
.catch((ex) => {
console.log(ex);});
console.log('4', this.scanCountries) ;
this.scanCountries variable (which is declared as public scanCountries : Countrymaster), outside .then() is loosing value and hence unable to use this in populating dropdown.
Updated Code:
this.globals.countryMasterDummy.then((data) => {
this.scanCountries = data;
this.countryLoaded = true;
// console.log('this.scanCountries',this.scanCountries);
})
.catch((ex) => {
console.log(ex);});
HTML:
<div *ngIf="countryLoaded ; else loading; ">
<span>Country</span>
<select class="form-control" [(ngModel)]="selectedOption">
<option [ngValue]="null">ALL</option>
<option *ngFor="let scanCountry of scanCountries" [value]="scanCountry.alpha2_Country_Code" >{{ scanCountry.Country_Name }}</option>
</select>
</div>