user3105189
user3105189

Reputation:

AngularJS: Using two-way data-binding to reload query

I'm trying to solve an issue where a two-way data-binding exists between a query being called with a parameter and that parameter dynamically changing with an input checkbox, but the query itself is not changing.

From the view:

<input type='checkbox' ng-model='checkbox' />

From the controller:

$scope.checkbox = false
$scope.items = Item.query({someKey: $scope.checkbox});

When checkbox is true, a small number of items are listed.
When checkbox is false, a large number of items are listed.

The checkbox can be toggled true and false, but it seems that it doesn't have an effect on the query.
Has someone ran into this issue before?

Upvotes: 0

Views: 117

Answers (1)

Phil Sandler
Phil Sandler

Reputation: 28046

You need to re-run the query each time the checkbox changes. Take a look at ngChange.

Something like:

<input type='checkbox' ng-model='checkbox' ng-change="requery()" />

$scope.requery = function(){ 
   $scope.items = Item.query({someKey: $scope.checkbox});
};

That may not be exactly right--usually you set your value ($scope.items) in the success method of an ajax call (I assume that's what Item.query does).

Upvotes: 2

Related Questions