【问题标题】:Angularjs change checkbox value according to server responseAngularjs根据服务器响应更改复选框值
【发布时间】:2017-09-16 06:40:13
【问题描述】:

我有两组复选框,用户可以在其中选择他们的选项并将其作为字符串保存到数据库中。

是否可以从服务器获取此字符串响应并将每个值设置回相应的复选框以保持选中状态?

<form class="list">

    <ion-toggle toggle-class="toggle-balanced" ng-model="data.turnOnOff" ng-true-value="'On'" ng-false-value="'Off'" ng-change="data.showConfirm(data.turnOnOff)">Databases</ion-toggle>
    <div class="spacer" style="height: 10px;"></div>

    <ion-list ng-show="bd">
        <ion-checkbox ng-repeat="(key, value) in db" ng-model=value.checked ng-change="checka()">{{ value.text }}</ion-checkbox>
    </ion-list>
    <div class="spacer" style="height: 10px;"></div>


    <ion-toggle toggle-class="toggle-balanced" ng-model="data.escritorio" ng-true-value="'On'" ng-false-value="'Off'" ng-change="data.openEscritorio(data.escritorio)">Office</ion-toggle>
    <div class="spacer" style="height: 10px;"></div>

    <ion-list ng-show="officee">
        <ion-checkbox ng-repeat="(key, value) in office" ng-model=value.checked>{{ value.text }}</ion-checkbox>
    </ion-list>
    <div class="spacer" style="height: 10px;"></div>
</form>
<div class="spacer" style="height: 10px;"></div>
<button style="color:#FFFFFF;" class="button button-balanced button-block" ng-click="insert()">Ok</button>

控制器

var id = localStorage.getItem("id");
var checkedData = [];

$scope.db = [
    {text:'Firebird', checked:'false'}, 
    {text:'MongoDB', checked:'false'}, 
    {text:'mSQL', checked:'false'}, 
    {text:'MySQL', checked:'false'}, 
    {text:'Oracle', checked:'false'}, 
    {text:'PostgreSQL', checked:'false'}, 
    {text:'TinySQL', checked:'false'}, 
    {text:'SQLite', checked:'false'}, 
    {text:'SQL Server', checked:'false'}, 
    {text:'Sybase', checked:'false'}, 
    {text:'Outros', checked:'false'}
]

$scope.office = [
    {text:'Microsoft Access', checked:'false'}, 
    {text:'Microsoft Excel', checked:'false'}, 
    {text:'Microsoft Outlook', checked:'false'}, 
    {text:'Microsoft PowerPoint', checked:'false'}, 
    {text:'Microsoft Word', checked:'false'}, 
    {text:'Open Office', checked:'false'}
]

$scope.data.turnOnOff = 'Off';
$scope.data.showConfirm = function(val){
    if(val === 'On'){
        $scope.bd = true;
    }else{
        $scope.bd = false;
    }
}

$scope.data.officee = 'Off';
$scope.data.openEscritorio = function(val){
    if(val === 'On'){
        $scope.officee = true;
    }else{
        $scope.officee = false;
    }
}

$scope.insert = function(){

    $ionicLoading.show({
        content: 'Loading',
        animation: 'fade-in',
        showBackdrop: true,
        maxWidth: 200,
        showDelay: 0
    });

    angular.forEach($scope.db, function(key, value){
        if(key.checked == true){
            //checkedData.push(key.text);
            checkedData += key.text + ', ';
            console.log("database "+checkedData);
        }
    });

    angular.forEach($scope.office, function(key, value){
        if(key.checked == true){
            checkedData += key.text + ', ';
            console.log("databases "+checkedData);   
        }
    });

    checkedData = checkedData.substring(0, checkedData.length - 2);
    console.log("result: "+checkedData);

    InformaticaFac.getData(checkedData, id).then(function(response){

        console.log(JSON.stringify(response));

        if(response.data === "\"Error\""){
            $ionicLoading.hide();
            navigator.notification.alert("Error");

        }else{

            checkedData = response.data[0].DATABASERESP;

        }
    }, function(response){
            navigator.notification.alert("Error.");

    }).finally(function(){
        $timeout(function(){

            $ionicLoading.hide();                
            $state.go('page3');

        }, 2000); 
    });

}

回复

checkedData = response.data[0].DATABASERESP;

"data":{"checkedData":"Firebird, MySQL, Oracle, Microsoft Excel, Microsoft Outlook, Microsoft PowerPoint, Microsoft Word","id":"1"}

【问题讨论】:

  • 我找不到在控制器代码中存储字符串响应的变量在哪里。另请提供字符串响应示例。
  • @Leguest 抱歉。请阅读已编辑的问题
  • 当询问由代码引起的问题时,如果您使用尽可能少的代码但仍会产生同样的问题,您将得到更好的答案。见How to create a Minimal, Complete, and Verifiable example

标签: angularjs checkbox ionic-framework


【解决方案1】:

我明白了,所以你可以试试这个:

  var checkedData = response.data

  checkedData.checkedData.forEach(function(checkedName) {

      $scope.db.forEach(function(dbItem){

          if(dbItem.text == checkedName) {
                dbItem.checked = true;
          }

      })

      $scope.office.forEach(function(officeItem){

          if(officeItem.text == checkedName) {
                officeItem.checked = true;
          }

      })

  })

  // if you do it in .then(function) or any async function you need to call
  $scope.$apply() // to update checkboxes state

更新

您忘记将字符串拆分为数组:

   checkedData.data.checkedData = checkedData.data.checkedData.split(', ');

所以现在我们可以做.forEach函数

JSFiddle example

【讨论】:

  • 几乎运行良好。它给了我 SyntaxError: Unexpected token F in JSON at position 0 在 JSON.parse 行。如果 checkedData = response.data; 我可以得到对象
  • 是的,我写这行是为了以防万一,你可以轻松摆脱它。我更新了我的答案
  • TypeError: 无法读取 checkData.checkedData.forEach(function(checkedName) 行上未定义的属性“forEach”
  • 这是我得到的响应:[{"ID":"1","DATABASERESP":"Firebird, MongoDB, Open Office"}]
  • 我试过 angular.forEach(checkedData, function(checkedName) 但复选框没有改变。
【解决方案2】:

我不得不做出一些改变,但根据 Leguest 的回答,我的最终结果是:

checkedData= response.data[0].DATABASERESP;
console.log(checkedData);

checkedData = checkedData.split(', ');
console.log(checkedData);

angular.forEach(checkedData, function(checkedName){

    $scope.db.forEach(function(dbItem){

        if(dbItem.text == checkedName) {
            dbItem.checked = true;
        }

   });

   $scope.office.forEach(function(officeItem){

       if(officeItem.text == checkedName) {
            officeItem.checked = true;
       }

   });

});

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多