从Promise中提取实际数据并分配给变量

时间:2019-11-25 16:14:08

标签: angular

我第一次尝试使用角度的Promise功能。我正在尝试将Promise中的值分配给变量,但是我无法使其正常工作。我想使用此最终变量在“选择”下拉列表中填充国家/地区数据。

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) ; 
.then()之外的

this.scanCountries变量(声明为 public scanCountries:Countrymaster )正在丢失值,因此无法在填充下拉列表中使用它。

随附控制台消息: enter image description here

更新的代码:

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>

0 个答案:

没有答案