Angular 4复选框更改值 [英] Angular 4 checkbox change value

查看:124
本文介绍了Angular 4复选框更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 4中如何实现在复选框中注册时保存"A"或"B"值.尽我所能,他只是寄给我是非还是错,希望有人能帮助我.

how could you achieve in Angular 4 that when you register in a checkbox save an "A" or "B" value. As much as I try, he is only sending me true or false, I hope someone can help me.

registry.component.ts

registry.component.ts

  this.userForm = new FormGroup({
   state: new FormControl('',),

  });

registry.component.html

registry.component.html

<div class="form-group">
                <label>State</label>
          <input type="checkbox" [(ngModel)]="isChecked" (change)="checkValue(isChecked?'A':'B')" formControlName="state"/>
        </div>  

            <pre>{{userForm.value | json}}</pre>

这样,我可以使控制台显示我想要的值(A或B),但在JSON中仍然是true或false.

That way I can get the console to show the value I want (A or B) but in the JSON is still true or false.

推荐答案

这就是您要寻找的:

<input type="checkbox" [(ngModel)]="isChecked" (change)="checkValue(isChecked?'A':'B')" />

在班上:

checkValue(event: any){
   console.log(event);
}

还在app.module.ts中包含FormsModule以使ngModel正常工作!

Also include FormsModule in app.module.ts to make ngModel work !

希望有帮助!

这篇关于Angular 4复选框更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆