简体   繁体   English

选择选项后调用函数的正确方法。 角 4

[英]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.

相关问题 单击新选项后,如何清除上一个选项的结果。 它不断将自身附加到上一个选项 - How do I clear the results of the previous option selected after clicking on a new option. It keeps appending itself to the previous option Vuejs 选项。 从所选项目中获取 ID - Vuejs Option. Get an ID from a selected item 从 matchip angular 中的选定属性调用函数的有效方法 - Efficent way to call a function from a selected attribute in matchip angular 有没有办法在“ resumelayouts”事件处理完成后立即调用函数? - Is there a way to call a function right after 'resumelayouts' event processing finishes? 这是在javascript中调用函数的正确方法吗? - Is this the right way to call a function in javascript? 选中的选项必须在onchange功能之后显示 - The selected option must shown after the function of onchange 如果选择下拉菜单,则span将显示该选项的属性。 这一直有效,直到我添加第二个“选择”输入 - if dropdown is selected span is to show attribute of that option. This works until I add a second Select input JSX 选择元素似乎没有在选项上自动选择(实现“已选择”)。 我错过了什么吗? - JSX select element does not seem to auto-select (implement 'selected') on option. Am I missing something? 调用obj函数不是正确的方法吗? - Is it not a right way to call my obj function? 选择了角空白选项 - Angular blank option selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM