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.
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.
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.
(ngModelChange)='onOptionsSelected($event)'
onOptionsSelected($event){
console.log($event);
}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With