Not able to get Angular 2 dropdown current value in ngModel

1k views Asked by At

I want to get selected value dropdown based on value of ngModel:

<div class="form-group">
                  <div>
                  <label class="dropdown" for="profile">Profile <span style="color:red">*</span></label>
                  </div>
                  <div class="half">
                  <select class="form-control" name='profile' [(ngModel)]='users.profile._id' required>
                        <option *ngFor="let obj of profiles"  [value]="obj._id">{{obj.profile}}</option>
                  </select>
                  </div>
            </div>
2

There are 2 answers

0
GsMalhotra On

Thanks @Günter Zöchbauer and @Stefan Svrkota

I have intialize this.users.profile='' as string previously which was my mistake it should be inialize as object this.users.profile={} ,remaining code is working.

4
Stefan Svrkota On

When using NgModel directive for dropdown lists, you should always use NgValue directive instead of value for list items (options):

<select class="form-control" name='profile' [(ngModel)]='users.profile._id' required>
    <option *ngFor="let obj of profiles" [ngValue]="obj._id">
        {{obj.profile}}
    </option>
 </select>

This should fix your problem.