2016-11-25 25 views
1

我有以下JS和HTML:AngularJs数组绑定到一个复选框

$scope.loadInstallation = function (installationid) { 
 
    $scope.currentInstallation = {}; 
 
    $scope.currentInstallation = $scope.installationList[installationid];; 
 
    $scope.currentInstallationID = installationid; 
 
};
<div class="row"> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList1">Module 1:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList1" ng-model="currentInstallation.moduleIdList" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList2">Module 2:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList2" ng-model="currentInstallation.moduleIdList" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList3">Module 3:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList3" ng-model="currentInstallation.moduleIdList" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div> 
 
<div class="row"> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList4">Module 4:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList4" ng-model="currentInstallation.moduleIdList" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList5">Module 5:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList5" ng-model="currentInstallation.moduleIdList" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList6">Module 6:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList6" ng-model="currentInstallation.moduleIdList" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div>

而一个JSON,这是这个样子:

currentInstallation = 
{ 
    "id":"1", 
    "moduleIdList": [ "1", "2" ] 
} 

的“在模块1:“复选框中,”moduleIdList“中的”1“等于”true“,但问题在于,我不知道如何将”moduleIdList“绑定到复选框,以便在出现”1“在“m oduleIdList“复选框被选中,当有人取消选中时,”1“将从阵列中删除

希望您能理解我的问题,我很乐意为您提供帮助!

问候, 西蒙

+0

似乎对我来说,那只是使用默认的'ng-model' - >这是不可能的。可能的办法是改变你的'moduleIdList'数组 – xAqweRx

+0

是的,我知道,它不符合ng-model,所以我正在寻找解决方法,因为不幸的是我无法更改moduelIdList array – TZimon

+0

简单的解决方法是在你的'Controller'函数中执行,它将从你的模型转换并更正'moduleIdList'版本。就这样。普通的'for-loop' – xAqweRx

回答

0

好吧,我得到了一个解决办法,这是为我好。 我只是不使用NG-模型,而不是我用NG-NG检查点击,这样我就可以使用的功能,这将做什么我想:

$scope.loadInstallation = function (installationid) { 
 
    $scope.currentInstallation = {}; 
 
    $scope.currentInstallation = $scope.installationList[installationid];; 
 
    $scope.currentInstallationID = installationid; 
 
}; 
 

 
$scope.isModuleSelected = function (id) { 
 
    if ($scope.currentInstallation.moduleIdList != null && $scope.currentInstallation.moduleIdList.indexOf(id) != -1) 
 
     return true; 
 
    else 
 
     return false; 
 
}; 
 

 
$scope.toggleModule = function (id) { 
 
    if ($scope.currentInstallation.moduleIdList == null) 
 
      $scope.currentInstallation.moduleIdList = []; 
 
    var numberOnIndex = $scope.currentInstallation.moduleIdList.indexOf(id); 
 
    if (numberOnIndex == -1) 
 
     $scope.currentInstallation.moduleIdList.push(id); 
 
    else 
 
     $scope.currentInstallation.moduleIdList.splice(numberOnIndex, 1); 
 
};
<div class="row"> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList1">Module 1:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList1" ng-checked="isModuleSelected('1')" ng-click="toggleModule('1')" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList2">Module 2:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList2" ng-checked="isModuleSelected('2')" ng-click="toggleModule('2')" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList3">Module 3:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList3" ng-checked="isModuleSelected('3')" ng-click="toggleModule('3')" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div> 
 
<div class="row"> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList4">Module 4:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList4" ng-checked="isModuleSelected('4')" ng-click="toggleModule('4')" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList5">Module 5:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList5" ng-checked="isModuleSelected('5')" ng-click="toggleModule('5')" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
    <div class="col-md-4"> 
 
     <div class="form-group"> 
 
      <label for="installationmoduleIdList6">Module 6:</label> 
 
      <div class="form-control"> 
 
       <label><input type="checkbox" id="installationmoduleIdList6" ng-checked="isModuleSelected('6')" ng-click="toggleModule('6')" /></label> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div>