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