2

angular4

having trouble with select [ngValue] showing selected value from model

<select [(ngModel)]="model.o">
<option *ngFor="let o of objs" [ngValue]="o"></option>
</select>

works fine but does not show selected object from model. but if i change [ngValue] to [value] it shows selected but then pass value as a string.

can some one help pls.

d-man
  • 57,473
  • 85
  • 212
  • 296
  • 2
    Possible duplicate of [SELECT --> OPTION, using value vs ngValue](https://stackoverflow.com/questions/43442920/select-option-using-value-vs-ngvalue) – Harry Ninh Aug 07 '17 at 23:34
  • You can also use `[compareWith]` with Angular v 4. Just disregard the formcontrol in the answer from the link, and use ngmodel instead. But you get the idea from the answer on how to use it :) https://stackoverflow.com/a/45370348/6294072 – AT82 Aug 09 '17 at 13:29
  • [compareWith] worked for me – d-man Aug 10 '17 at 14:04

2 Answers2

3

able to get it work using [compareWith] attribute.

d-man
  • 57,473
  • 85
  • 212
  • 296
-4

Try This

<select name="model" [(ngModel)]="model.o">
    <option *ngFor="let o of objs" value="{{o}}">{{o}}</option>
</select>
Lucifer
  • 784
  • 2
  • 7
  • 20
  • Could you please suggest with [ngValue] instead of value as it doesn't take object. please refer below link `https://www.reddit.com/r/Angular2/comments/65run4/select_option_using_value_vs_ngvalue/`. i want to disable inital value in dropdown with ngValue – Prasanna Feb 06 '18 at 07:41