ARR.s
ARR.s

Reputation: 769

How to set selected ngfor ionic2

I want to set selected but I tried this below it's not work How to set selected ngfor ionic2 I tried

<ion-item>
      <ion-label>Source</ion-label>
      <ion-select [(ngModel)]="filter" >
        <ion-option value={{item.val}} *ngFor="let item of options" selected="item.val == 'pencil'">{{item.name}}</ion-option>
      </ion-select>
    </ion-item>

it's not work ,not show selected this to default

ts

public options =  [
        {
            "name": "Pencils",
            "val" : "pencil"
          }
             .
             .
             .

        ];

Upvotes: 2

Views: 3974

Answers (2)

El houcine bougarfaoui
El houcine bougarfaoui

Reputation: 37373

[selected] instead of selected and [value] instead of value :

  <ion-option [value]="item.val" *ngFor="let item of options" [selected]="item.val == 'pencil'">{{item.name}}</ion-option>
      </ion-select>

note :

  1. use property when bind to static value ex: value="someValue"
  2. use [property] when bind to a variable or expression ex: [value]="getSomeValue()".

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

Since you have ngModel, you should set the filter to the selected item initially:

public filter = this.options[0].val;

Upvotes: 2

Related Questions