mercredi 2 septembre 2015

ngModel checkbox not shown as selected

I'm having some trouble getting a checkbox to display the correct state (checked/unchecked) of my model. I have the following in my controller:

app.controller('PhotosCtrl', ['$scope', function($scope) {
    $scope.form = {};
    $scope.idArray = {};
    $scope.editPhotos = {
        token: $scope.token,
        ids: $scope.idArray
    };
}]);

My form looks like this:

<form accept-charset="UTF-8" name="editPhotosForm" ng-submit="submit(postQuestion);" multipart="true" novalidate>
    <input name="utf8" type="hidden" value="✓">
    <input name="authenticity_token" type="hidden" ng-model="editPhotos.token" ng-init="editPhotos.token='<%= form_authenticity_token %>'">
    <div class="results-label"><b>{{total_count}}</b> <span ng-if="total_count == 1">Photo</span><span ng-if="total_count != 1">Photos</span> Found</div>
    <div>
        <div class="col">
            <a ng-repeat="r in results" class="card result-link" ng-init="idArray[r.id] = 'false'">
                <div class="content result">
                    <div class="caption">
                        <input type="checkbox" ng-model="idArray[r.id]" ng-true-value="true" ng-false-value="false">
                    </div>

                    <div class="image-container" ng-style="{'background-image': 'url(' + r.image_url + ')'}">
                    </div>
                </div>
            </a>
        </div>
    </div>
</form>

On my repeater element, I call ng-init="idArray[r.id] = 'false'" to initialize a key r.id = 'false' for each item in r. I don't know if I need to do this or not. I've tried the code without it and it makes no difference. I've also tried using ng-value-true and ng-value-false but those don't seem to be working for me.

Most of the other posts I've seen on this issue deal with simple variables (e.g. $scope.someVar = true rather than more complex structures like a hash.




Aucun commentaire:

Enregistrer un commentaire