Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4 on change select box returns wrong value

Tags:

angular

I want to do something in change event, but change event is returning a value with colon prefix. How to get correct value without prefix and colon?

education.component.html

<select class="form-control custom-select" name="course_id" id="course_id" formControlName="course_id" (change)="onChange($event.target.value)">
    <option value="">--Select--</option>
    <option *ngFor="let course of course_list" [ngValue]="course.id">{{ course.name }}({{course.id}})</option>
</select>

education.component.ts

onChange(value) : void {
   console.log('Course Value',value)
}

For Example: I'm getting the value 2:8. Expected value is 8

like image 732
Rajasekar D Avatar asked May 14 '17 17:05

Rajasekar D


1 Answers

You just use [ngValue] when you want to bind objects.

So, as value.id is a primitive value, use [value] instead of [ngValue]:

<option *ngFor="let course of course_list" [value]="course.id">{{ course.name }}({{course.id}})</option>
like image 149
developer033 Avatar answered Oct 13 '22 02:10

developer033