如何在Angular 5应用程序中获取复选框值 [英] How to get checkbox value in angular 5 app
问题描述
我正在使用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屋!