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“将从阵列中删除
希望您能理解我的问题,我很乐意为您提供帮助!
问候, 西蒙
似乎对我来说,那只是使用默认的'ng-model' - >这是不可能的。可能的办法是改变你的'moduleIdList'数组 – xAqweRx
是的,我知道,它不符合ng-model,所以我正在寻找解决方法,因为不幸的是我无法更改moduelIdList array – TZimon
简单的解决方法是在你的'Controller'函数中执行,它将从你的模型转换并更正'moduleIdList'版本。就这样。普通的'for-loop' – xAqweRx