Failed to set (option selected property) to true after promise call


#1

Hey everyone
after getting json data from WCF service and filling select component i can’t set option propery to true

This is my load funtion

loadProductGrpID()
  {
    this.jsonService.loadProductGrpID().then(res => {
      this.ProductGrpID = JSON.parse(res['GetTableResult']);
    });
  }

This is my html

<ion-item>
    <ion-label>Product group id :</ion-label>
    <ion-select formControlName="ProductGrp" >
      <ion-option *ngFor="let c of ProductGrpID;let i = index" value="{{ c.id }}" selected="{{ c.is_default }}">{{ c?.item_name }}</ion-option>
    </ion-select>
  </ion-item>

#2

try this instead:

<ion-option *ngFor="let c of ProductGrpID;let i = index" [value]="{{ c.id }}" [selected]="{{ c.is_default }}">{{ c?.item_name }}</ion-option>

#3

I don’t see you using i, so I would get rid of it. You are using the Elvis operator inconsistently. If you’re going to use it at all, you must use it everywhere (which is partly why I recommend not using it at all). If you are not initializing ProductGrpID to an empty array, you need to do so. Finally, don’t try to set selected on options: instead, set the value of the backing control in the form to the value of whatever should be selected.