-1
**Here is my code**
<td>
    <label class="col-md-2">
      <input type="checkbox" class="form-control" [ngModel]="row.create" name="create" [checked]="(row.create==='Y')" (change)="onChecked(operation, appName, 'create', $event.target.checked)">
    </label>
   <label class="col-md-2">
    <input type="checkbox" class="form-control" [ngModel]="row.read" name="read" [checked]="(row.read==='Y')" (change)="onChecked(operation, appName, 'read', $event.target.checked)">
  </label>
    <label class="col-md-2">
      <input type="checkbox" class="form-control" [ngModel]="row.exportFile" name="exportFile" [checked]="(row.exportFile==='Y')" (change)="onChecked(operation, appName, 'exportFile', $event.target.checked)">
    </label>
    <label class="col-md-2">
      <input type="checkbox" class="form-control" [ngModel]="row.exportData" name="exportData" [checked]="(row.exportData==='Y')" (change)="onChecked(operation, appName, 'exportData', $event.target.checked)"> 
    </label>                                
</td>

 **Here is component.ts**
private onChecked(operation:any, appName: any, checked:boolean){        
    let appaccess = this.appData.find(
   appaccess=>appaccess.appName===appName
   );
  let cbindex = this.appData.indexOf(appaccess);

    if(checked){
        switch(operation)
        {
            case 'create':
                this.appData[cbindex].create = 'Y';
                break;
            case 'read':
                this.appData[cbindex].read = 'Y';
                break;
            case 'exportFile':
                this.appData[cbindex].exportFile = 'Y';
                break;
            case 'exportData':
                this.appData[cbindex].exportData = 'Y';
                break;
            case 'publish':
                this.appData[cbindex].publish = 'Y';
                break;
        }    
        console.log('option checked is '+ operation);        
    }
    else{
        switch(operation)
        {
            case 'create':
                this.appData[cbindex].create = 'N';
                break;
            case 'read':
                this.appData[cbindex].read = 'N';
                break;
            case 'exportFile':
                this.appData[cbindex].exportFile = 'N';
                break;
            case 'exportData':
                this.appData[cbindex].exportData = 'N';
                break;
            case 'publish':
                this.appData[cbindex].publish = 'N';
                break;
        }
        console.log('option unchecked is '+ operation);
    }     
}

I have some data in database with value of "Yes" and "No". When i checked need to send yes value and when unchecked send no. Please any write function for that. and I need to compare same in to database also. Please help me on this. Thanks in advance.

2

2 Answers 2

2

Template side :

<input type="checkbox" class="form-control" name="" id=""  (change)="onChecked($event.target.value)">

Component Side :

onChecked(value : boolean)
{
    // value will always be true/false
    // as per your database call pass the value
    if(value)
    {
        // make a call for checked
    }
    else
    {
        // make a call for unchecked
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can any one please check once again.
1

Or the shortest way:

<input type="checkbox"
    (change)="profile.ActiveYN = $event.target.checked ? 'Y' : 'N'"
    [checked]="profile.ActiveYN == 'Y'" />

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.