1

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);
  }
4
  • You need the true or false values ? Commented May 24, 2018 at 9:29
  • The snippet looks like AngularJs(v1.x)... not Angular (v2.0+) Commented May 24, 2018 at 9:29
  • yes................ Commented May 24, 2018 at 9:29
  • i need angular 2 implementation Commented May 24, 2018 at 9:30

1 Answer 1

2

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?
Sign up to request clarification or add additional context in comments.

4 Comments

[checked]="check" return undefinded when I console.log in component
is check defined in your component. If not, just stick with the two way binding method of using ngModel or add check: boolean to your component
[(ngModel)]="check" worked, but still how to read value from $event for checkbox??
You don't need to read the value from the checkbox, your model is check, you only need to look at the value for check now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.