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

GCP
  • 69
  • 3
  • 10
  • Do you have a service in place? If so, just call your service from onChecked() method with data from $event. Check this answer: http://stackoverflow.com/questions/37077707/launch-an-event-when-checking-a-checkbox-in-angular2 In case you're not familiar with services - go to Angular tutorial: https://angular.io/docs/ts/latest/tutorial/toh-pt4.html – borkovski Mar 30 '17 at 07:12
  • 1
    [so] is *not* a free code writing service. You are expected to try to **write the code yourself**. After [doing more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a **[mcve]**. I suggest reading [ask] a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour]. – ProgrammingLlama Mar 30 '17 at 07:14

2 Answers2

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
    }
}
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
1

Or the shortest way:

<input type="checkbox"
    (change)="profile.ActiveYN = $event.target.checked ? 'Y' : 'N'"
    [checked]="profile.ActiveYN == 'Y'" />
baHI
  • 1,510
  • 15
  • 20