[英]right way to call a function after selected option. Angular 4
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.
我使用 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>
You can use the change event handler as folows, which passes the selected value to the handler: 您可以将change事件处理程序用作以下项,它将选择的值传递给处理程序:
<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. 您还可以使用[[ngModel)]将select的值绑定到模型上的属性,然后就不需要将值传递给处理程序了,因为您的模型已经拥有了该值。
https://angular.io/api/forms/NgModel https://angular.io/api/forms/NgModel
The Angular way to approach this in a dropdown would be to allow Angular to manage the value with ngModel
, and have Angular watch for the ngModel
value to change by using ngModelChange
. Angular在下拉列表中解决此问题的方法是允许Angular使用
ngModel
管理值,并让Angular使用ngModelChange
监视ngModel
值的更改。
This is how it would look with the example: 该示例的外观如下:
<select [(ngModel)]="chosenMod" (ngModelChange)="modo()" 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>
In your component you would add a variable declaration for the chosen dropdown value, and then reference that in your modo
method: 在您的组件中,您将为选定的下拉值添加一个变量声明,然后在您的
modo
方法中引用它:
chosenMod: string = "";
modo(){
switch(this.chosenMod) {
case "mod1": {
//do something
break;
}
case "mod2": {
//do something
break;
}
case "mod3": {
//do something
break;
}
}
}
What will happen is because of the two-way binding on chosenMod
, Angular will be watching for the value to change in the dropdown, and it will update chosenMod
when the dropdown selection changes. 将会发生这种情况是因为对
chosenMod
进行了双向绑定,Angular将监视下拉列表中的值更改,并且当下拉列表选择更改时,它将更新chosenMod
。
When you bind to the ngModelChange
event, Angular will watch for the value of the ngModel
declaration to change, and it will run the code specified. 当您绑定到
ngModelChange
事件时,Angular将监视ngModel
声明的值是否更改,并且它将运行指定的代码。
The combination of these two tags will mean that as soon as the selection changes, Angular will update the value of chosenMod
, and then execute the modo()
method. 这两个标记的组合将意味着,只要更改选择,Angular就会更新
chosenMod
的值,然后执行modo()
方法。
Since you've bound to the component variable chosenMod, you don't need to send the value of the selection into the modo method you will be executing. 由于已绑定到组件变量selectedMod,因此无需将选择的值发送到将要执行的modo方法中。
If you use ngModel
you can use ngModelChange
如果使用
ngModel
,则可以使用ngModelChange
<select ngModel (ngModelChange)="doSomething($event)" 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>
In angular 4+ you should write select code like this 在angular 4+中,您应该编写这样的选择代码
<select [ngModel]="info" (onModelChange)="function()">
<option [ngValue]="">select</option>
<option *ngFor="info of infoSelect">{{info}}</option>
</select>
This is the best way to call function on select 这是在select上调用函数的最佳方法
use (selectionChange)使用(选择更改)
example:例子:
<mat-select [(value)]="value" (selectionChange)=updateCategory()>
<mat-option *ngFor="let name of names" [value]="name">
name
</mat-option>
</mat-select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.