1

I am working on Angular 5 application with checkbox. I have data coming from database, followed by mapped to javaScript object. I am trying to configure 'Selected' value but unable to do so. In my result, I got all boxes checked which should be.

in following code, option: [] --> 'optionSelected' defined if is selected value by true or false,

produced schema as following;

enter image description here

Template

 <div *ngSwitchCase="'checkbox'"> <small>checkbox</small>
       <div *ngFor="let opt of question.options" >
            <label class="container-vertical"> {{opt.value}}
                <input type="checkbox" [formControlName]="question.key" [name]="opt.name" [value]="opt.key" [checked]="opt.optionSelected" /> 
                <span class="checkmark2"></span>
            </label>
       </div>      
 </div> 

also tried with ngModel as in my component, property 'value' holds id of selected key but still final result checked all boxes

<input type="checkbox" [formControlName]="question.key" [name]="opt.name" [value]="opt.key" [(ngModel)]="question.value" />

component

 else  if(questionElementType=="checkbox")
 {
   let _checkBox = new CheckBoxQuestion ({
     consultationId: questionsList[key].consultationId,
     questionId: questionsList[key].questionId,
     questionElementType: questionsList[key].questionElementType[0].title,           
     questionType: questionsList[key].questionType,
     title:questionsList[key].title,
     displayId: questionsList[key].displayId,
     key: questionsList[key].questionId,     
     value: questionsList[key].answer.length<=0? null : questionsList[key].answer[0].answerValue.toLowerCase(),
     label: questionsList[key].title, 
     order: 7,
     options: questionsList[key].answerOptions.map(function(item){
       return {"name": item.ReferenceKey, "key": item.preDefineAnswerOptionId, "value": item.text, "optionSelected": item.preDefineAnswerOptionId==questionsList[key].answer[0].answerValue? "true": "false"}
    }),
   });

     this.mappedQuestions.push(_checkBox);
 }

2 Answers 2

3

If you want the checkbox to be unchecked, its [value] binding must be falsy.

Both "true" and "false" are non empty strings, which means that they evaluate to true.

Convert "false" to false and it will work as expected.

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
<input type="checkbox" [(ngModel)]="checkbox1" /> "true"
<input type="checkbox" [(ngModel)]="checkbox2" /> "false"
<input type="checkbox" [(ngModel)]="checkbox3" /> true
<input type="checkbox" [(ngModel)]="checkbox4" /> false
  `,
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  checkbox1 = 'true';
  checkbox2 = 'false';
  checkbox3 = true;
  checkbox4 =  false;
}

checkbox example

Live demo

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help :)
1

You can use it

 <input type="checkbox" [(ngModel)]="active" [checked]="true" 
(change)="isAactiveChange($event)" >

Comments

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.