Andreas Elia Andreas Elia - 1 year ago 60
Javascript Question

Angular 1.5 Scope Variable not working from within PouchDB response function

So I've got the following code...

testApp.controller(...) {

$scope.results = [];
$scope.hasData = true;

"name": "test"
}); // WORKS

db.get('table_people').then(function(response) {

"name": "test"
}); // this DOESN'T WORK even though the "success" message is printed...


And as you can tell from the comments, the first
to the array works, but the latter one doesn't. Top one can be printed out in the Angular template using
{{ results }}
but the latter one returns an empty array.

Edit: A solution has been found by using
as the digest cycle wasn't running but sort of feels like a hacked together solution.

Edit: Solution...

db.get('table_people').then(function (response) {
$timeout(function () {
$scope.results =;

The solution code is slightly different as I no longer need the test data anymore due to the code functioning and can apply the response data directly.

Answer Source

You're missing a $digest cycle tick. Doing $scope.$digest() after you've pushed the data into $scope.results should fix the issue. Using $timeout is a bit of an overkill in this situation (and additional service to inject).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download