简体   繁体   English

第一次单击单选按钮不显示价值

[英]First click on radiobutton doesn't show value

when i first time click on radio button it doesn't show value 当我第一次单击单选按钮时,它没有显示值

<md-radio-group [(value)]="sex" layout="row" >
<md-radio-button value="Female" (click)="setSex(value)" >Female</md-radio-button>
<md-radio-button value="Male"  (click)="setSex(value)">Male</md-radio-button>

and i trying to get value in my class 我试图在课堂上获得价值

 public sex:String;
 public vm: String;  

  setSex(sex:String)  {
      if (this.sex) { 
        this.vm = this.sex;
         console.log(this.sex)
               }
       }

Example

Remove the click handlers: 删除点击处理程序:

 <md-radio-button value="Female">Female</md-radio-button>
 <md-radio-button value="Male">Male</md-radio-button>

Either bind to a model variable or use a click handler, but both is asking for trouble. 绑定到模型变量或使用单击处理程序,但是两者都在麻烦。

See here: http://plnkr.co/edit/e5Ko608lNDmasZGJFV1M?p=preview 看到这里: http : //plnkr.co/edit/e5Ko608lNDmasZGJFV1M?p=preview

You can't use value as the event argument (it is undefined ). 您不能将value用作事件参数(它是undefined )。 I suggest you create local template variables and then you can pass their values into the event handler: 我建议您创建本地模板变量,然后将其值传递给事件处理程序:

<md-radio-button #r1 value="Female" (click)="setSex(r1.value)">Female</md-radio-button>
<md-radio-button #r2 value="Male"   (click)="setSex(r2.value)">Male</md-radio-button>

and then this will work as desired: 然后它将按需要工作:

setSex(sex:String)  {
   console.log(sex);
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM