如何在离子选择2中选择第一个选项。选择的属性不起作用

如何在离子选择2中选择第一个选项。选择的属性不起作用

问题描述:

我试图从<ion-select>中选择第一个值,但它不起作用。它在第一(变量)中给出了真实。如果我在循环中打印它。它通常像我想的那样工作。如何在离子选择2中选择第一个选项。选择的属性不起作用

dashboard.html

<ion-list> 
<ion-item *ngIf="locations != null"> 
    <ion-label>Location</ion-label> 
    <ion-select name="locSelect" #newSelect [(ngModel)]="location" (ionChange)="changeStock(location)"> 
    <ion-option value="{{loc.id}}" *ngFor="let loc of locations let first = first; let last = last; let i = index;" [selected]="first">{{loc.location_name}}</ion-option> 
    </ion-select> 
</ion-item> 

dashboard.ts

getLocations(id) { 
     this.catService.getLocations(id, this.user.apiToken, this.user.role_id).then(result => { 
      this.locations = result; 

      if(this.user.role_id == 2) { 
       this.getDailyItemLogAdmin(this.firstLocationID, this.user.id); 
      }else if(this.user.role_id == 3) { 
       this.getDailyItemLogManager(this.firstLocationID, this.user.id); 
      } 

     }) 
    } 

我打电话上述的getLocations()从CONSTRU功能构造函数。

在您的dashboard.ts尝试设置ngModel变量location作为第一个位置。

this.catService.getLocations(id, this.user.apiToken, this.user.role_id).then(result => { 
      this.locations = result; 
      this.location=this.locations[0].id;//here 
      let breakIt = false; 
      for (let key in this.locations) { 
      //... 
+0

是的它的工作非常感谢 – Shivam