vendredi 25 novembre 2016

AngularJs bind an Array to an checkbox

i have the following JS and HTML:

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

And an JSON, which look something like this:

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

The "1" in the "moduleIdList" equals "true" in the HTML "Module 1:" Checkbox, the Problem is, I don't know, how to bind the "moduleIdList" to the Checkboxes, so that when there's a "1" in "moduleIdList" the Checkbox is checked and when someone uncheck it, the "1" will be deleted out of the Array

Hope you can understand my problem, I'm glad for any help!

Greetings, Simon




Aucun commentaire:

Enregistrer un commentaire