【问题标题】:If checkbox checked need to send "Yes" else send "No" to database with angular2?如果选中复选框需要发送“是”,否则使用 angular2 向数据库发送“否”?
【发布时间】:2017-08-23 23:07:51
【问题描述】:
**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);
    }     
}

我在数据库中有一些值为“是”和“否”的数据。当我选中需要发送是值时,当未选中时发送否。请为此编写任何函数。我还需要将相同的内容与数据库进行比较。请帮助我。提前致谢。

【问题讨论】:

标签: angular typescript


【解决方案1】:

模板方面:

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

组件端:

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

【讨论】:

  • 谁能再检查一遍。
【解决方案2】:

或最短路径:

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

【讨论】:

    猜你喜欢
    • 2020-12-11
    • 2012-02-08
    • 2020-04-18
    • 2014-02-23
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多