Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind selected value to dropdown list in Angular 4

Tags:

angular

I have a Dropdown list with a list of courses ..for example, one student selected one course and saved all the details. At the time of edit how we can bind selected course to drop down in Angular 4 anyone help me, please.

like image 807
Anil kumar Avatar asked Oct 25 '17 06:10

Anil kumar


People also ask

How do I get the value of the selected dropdown menu item with angular?

Angular has another way to get the selected value in the dropdown using the power of ngModel and two-way data binding. The ngModel is part of the forms module. We need to import it into the NgModule list in the app. module , which will be available in our app.

How do you bind a selected item in a drop down list to a text box and edit it?

You can bind a selected value from the drop-down list using the ngModel and edit it inside the text box or numeric input box. The selected item text and value can be got using the change event of the drop-down list through the args. itemData.


2 Answers

(ngModelChange)='onOptionsSelected($event)'

onOptionsSelected($event){
 console.log($event);
}
like image 176
Sanchita Sharma Avatar answered Sep 18 '22 23:09

Sanchita Sharma


You can do something like this :

<select class='select-option' required [(ngModel)]='optionSelected' (ngModelChange)='onOptionsSelected($event)'>
    <option class='option' *ngFor='let option of options' [value]="option">{{option}}</option>
</select>

So whenever option is changed , the value is stored in optionSelected in your ts,

options = [1, 2, 3];
optionSelected: any;

onOptionsSelected(event){
 console.log(event); //option value will be sent as event
}

(fixed functionName in template and the ts to be the same)

like image 20
CruelEngine Avatar answered Sep 18 '22 23:09

CruelEngine