I have an array on the scope that is being used to populate an ng-repeat with checkboxes inside.
I have a property in an object that I'm trying to set to contain that array filtered by checkboxes in the ng-repeat that have been checked.
Basic HTML:
<div ng-controller="MyCtrl">
<ul>
<li ng-repeat="box in boxes">
<input type="checkbox" ng-model="box.checked" ng-click="testingOnly()"/> {{box.label}}
</li>
</ul>
<div ng-repeat="object in objects.prop1">{{object.label}}</div>
<div ng-repeat="object in objects.prop2">{{object.label}}</div>
Basic JS:
function MyCtrl($scope, $filter) {
$scope.name = 'Superhero';
$scope.boxes = [{label: 'something else'}, {label: 'world'}]
$scope.objects = {
prop1: [{label: 'hello'}],
prop2: $filter('filter')($scope.boxes, {checked: true})
}
$scope.testingOnly = function() {
console.log($filter('filter')($scope.boxes, {checked: true}));
}
}
JSFiddle: http://ift.tt/1F2QsYg
I know that the filter is working based on the log output. I think I'm not understanding some fundamental part of how Angular is working. I was hoping to wire the two pieces together and that objects.prop2 would update without writing any additional helper function.
Is it possible to accomplish that, or will I have to write some updating code to run every time the checkbox is clicked?
Update: So I suppose the most simple change would be something like:
$scope.testingOnly = function() {
$scope.objects.prop2 = $filter('filter')($scope.boxes, {checked: true});
}
but I'm still curious if there is a way to do this without the function on the checkboxes
Aucun commentaire:
Enregistrer un commentaire