如何在Angular 5应用程序中获取复选框值 [英] How to get checkbox value in angular 5 app

查看:63
本文介绍了如何在Angular 5应用程序中获取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular复选框,需要读取给定的ng-true-value/ng-false-value或布尔值的值,不确定我的代码中缺少什么.我正在读取事件,但不确定要读取哪个值?

I am working on Angular checkbox and need to read value either given ng-true-value / ng-false-value or boolean value not sure what I am missing from code. I am reading event but not sure which value to read??

<div>

 <input 
    type="checkbox" 
    name="questionAnswerState" 
    ng-model="check"
    ng-true-value = "answerProvided"
    ng-false-value="questionAnswerNotProvided" 
    (change)="isAnswerProvided($event, check)"
    /> Answer Provided?

 isAnswerProvided(event: any, check:any)
  {
    console.log("question answer not provided responseId:: ",this.responseId, " questionId::",this.questionId, "  check::", check );
    console.log(event);
  }

推荐答案

如果您使用的是Angular 2>,则应使用checked属性进行单向绑定,即UI仅读取.使用此方法,您将必须更新组件中的check值.

If you're using Angular 2> you should use the checked attribute for using one way binding, that the UI will only read the value of check. Using this method you would have to update the check value in your component.

 <input 
    type="checkbox" 
    name="questionAnswerState" 
    [checked]="check"
    (change)="isAnswerProvided($event, check)"
    /> Answer Provided?

,或者如果您要进行双向绑定(状态完全由UI控制),则可以使用ngModel,如下所示:

or if you're after two way binding, where the state is controlled completely by the UI you can use ngModel like this:

 <input 
    type="checkbox" 
    name="questionAnswerState" 
    [(ngModel)]="check"
    (change)="isAnswerProvided($event, check)"
    /> Answer Provided?

这篇关于如何在Angular 5应用程序中获取复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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