jeudi 30 juin 2016

Auto select and collect checkbox values in array

I'm having a problem with setting (checking) and fetching array of data from view to my model. Let me explain my problem further with code.

This is my controller where i provide working days

$scope.workDays = [
                {name: 'Monday', value: 1},
                {name: 'Tuesday', value: 2},
                {name: 'Wednesday', value: 3},
                {name: 'Thursday', value: 4},
                {name: 'Friday', value: 5},
                {name: 'Saturday', value: 6},
                {name: 'Sunday', value: 7},
            ]
$scope.selectedDays = [1,3,4,6];

Then i render those check boxes with ng-repeat in my HTML

<label>Working days</label>
<div class="checkbox">
     <label ng-repeat="w in workDays" ng-model="myData">
            <input type="checkbox" ng-value="w.value" >
            
     </label>
</div>

1. Question: How to check checkboxes based on value $scope.selectedDays?

The second problem that i have is once i try to fetch new selected values i always get empty array

So my controller looks like this

$scope.myData = [];
console.log($scope.myData);

Then i select and deselect some checkboxes, how ever, console log reports empty array []

I hope you guys can help me. If you need any additional informations please let me know and i will provide. Thank you!




Aucun commentaire:

Enregistrer un commentaire