Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  87] [ 5]  / answers: 1 / hits: 45851  / 7 Years ago, wed, july 12, 2017, 12:00:00

Hello I need to call a function after one of the options is selected.



which is the best way to do it? im using angular4.





modo(){
// if modo 1 is selected do something.
// if modo 2 is selected do something.
// if modo 3 is selected do something.
}

<label>Modo :</label>
<select id=selectid class=form-control-mb-12>
<option value=mod1>MODO 1</option>
<option value=mod2>MODO 2</option>
<option value=mod3>MODO 3</option>
</select>




More From » angular

 Answers
9

You can use the change event handler as folows, which passes the selected value to the handler:



<select id=selectid class=form-control-mb-12 (change)=modo($event.target.value)>
<option value=mod1>MODO 1</option>
<option value=mod2>MODO 2</option>
<option value=mod3>MODO 3</option>
</select>

modo(value: string){
switch(value) {
case mod1:
// if modo 1 is selected do something.
break;
case mod2:
// if modo 2 is selected do something.
break;
case mod3:
// if modo 3 is selected do something.
break;
}
}


You could also bind the value of the select to a property on your model using [(ngModel)] then you wouldn't need to pass the value to the handler as your model would already have it.



https://angular.io/api/forms/NgModel


[#57107] Monday, July 10, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
finn

Total Points: 154
Total Questions: 88
Total Answers: 82

Location: Lithuania
Member since Mon, Nov 16, 2020
4 Years ago
;