Angular 2 radio button events

﹥>﹥吖頭↗ 提交于 2020-01-22 13:18:32

问题


What are those events called in Angular 2 when radio button is selected or unselected.

Something like

<input type="radio" (select)="selected()" (unselect)="unselected()" />

So when I click one radio button in a group, it will fire selected() for the new selection and unselected() for the previous selection.


回答1:


It works,

<input type="radio" (change)="handleChange($event)" />

But you need code more to judge 'selected' or 'unselected'.
You may try this in your *.ts file:

  export class Comp {

    private _prevSelected: any;

    handleChange(evt) {
      var target = evt.target;
      if (target.checked) {
        doSelected(target);
        this._prevSelected = target;
      } else {
        doUnSelected(this._prevSelected)
      }
    }

  }



回答2:


It works when you assign the click event to the label, instead of the input.




回答3:


The html is like

 <div *ngFor = " let displayParameter of displayParameters" >    
    <!-- <li><a href="#">{{displayParameter}}</a></li>     -->
    <!-- <input type="radio"  name="displayParameter"  (change) ="handleChange(event)")>  -->
    <h5><input type="radio" name="radiogroup" (change)="handleChange(displayParameter)" [checked]="(idx === 0)" >{{displayParameter}} </h5>
</div>

and code is like

 handleChange(evt){ 
            this.displayParameter = evt;
        console.log(evt);
      } 


来源:https://stackoverflow.com/questions/37541106/angular-2-radio-button-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!