Home > database >  How to check checkbox base on Value received
How to check checkbox base on Value received

Time:05-28

Understanding Lets say i received value "Yes" and "No". My question is i want to check the checkbox only when i received "Yes".

codes that i tried

<section  *ngFor="let item of patientPastMedicalHistoryList">
              <mat-checkbox color="primary"
              (change)="patientPastHistoryForm.get('substanceAbuseAlcohol').setValue(
                $event.checked ? 'Yes' : 'No')" 
                checked="{{item.substanceAbuseAlcohol ==='Yes'}}" 
               >Alcohol</mat-checkbox><br>
              </mat-form-field>
</section> 

I have tried This doesnt help. Below code always check the checkbox even when the value received is "NO"

[checked]="patientPastHistoryForm.get('substanceAbuseAlcohol').value=='Yes'"

 checked="{{item.substanceAbuseAlcohol ==='Yes'}}"

form

this.patientPastHistoryForm = new FormGroup({
      patientId: new FormControl(this.clientId),
      substanceAbuseAlcohol: new FormControl(''),})

data

 patientPastMedicalHistoryList: Array<PatientPastMedicalHistoryModel>=[];

This is how i fetch the value

  getPatientPastHistoryList() {
    debugger
    this.clientsService.getPatientPastHistoryList(this.clientId)
      .subscribe(
        (response: ResponseModel) => {
          if (response.statusCode === 200) {
            this.patientPastMedicalHistoryList = response.data;                                
          } else {
            this.patientPastMedicalHistoryList = [] ;           
          }
          this.patientPastHistoryForm.patchValue({
            ...response.data,
          })
        });
  }

Received data

{
  "expires_in": 0,
  "data": [
    {
      "id": 0,
      "patientId": 2143,
      "substanceAbuseAlcohol": "No",
      "substanceAbuseMarijuana": "Yes",
}

CodePudding user response:

Stackblitz

response.data is an array

you want this.patientPastHistoryForm.patchValue({ ...response.data[0], })

  • Related