2

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" } . . . ]; 
4
  • What doesn't work? Do you get an error? What happens? Where are you setting options? Commented Mar 29, 2017 at 16:25
  • please update the error it's throwing in the console. ! Commented Mar 29, 2017 at 16:26
  • @ExplosionPills I editted the posts Commented Mar 29, 2017 at 16:31
  • @rrmerugu it's not error but it's not show selected "pencil" to default Commented Mar 29, 2017 at 16:32

2 Answers 2

2

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

public filter = this.options[0].val; 
Sign up to request clarification or add additional context in comments.

4 Comments

Great!. but, why does selected doesn't work ! ionicframework.com/docs/v2/components/#select shows that selected="seomthing true" should work !
@rrmerugu [selected]="seomthing true" use this instead because its a expression
ohh I have not tried yet >>> [selected]="seomthing true"
@ARR.s I believe this is just an issue with the Ionic documentation and that the ngModel binding will not work with selected
0

[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()".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.