How to get checkbox value in angular 5 app

只愿长相守 提交于 2019-12-23 02:29:35

问题


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??

template

<div>

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

component

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

回答1:


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?

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?


来源:https://stackoverflow.com/questions/50505612/how-to-get-checkbox-value-in-angular-5-app

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