So, take a look at this code snippet...
testApp.controller(...) {
$scope.results = [];
$scope.hasData = true;
$scope.results.push({
"name": "test"
}); // This part works fine
db.get('table_people').then(function(response) {
console.log('success');
$scope.results.push({
"name": "test"
});
}); // This section seems to be causing trouble although the success message is displayed...
});
As you can see from the comments, the first push
operation adds data successfully to the array, but the second one fails. The former data can be rendered in the Angular template using {{ results }}
, however, the later push returns an empty array.
Edit: To address this issue, a workaround involving the use of $timeout
was implemented as it appeared that the digest cycle was not triggering properly. Although a bit of a makeshift solution, it does resolve the problem.
Edit: Here's the updated solution...
db.get('table_people').then(function (response) {
console.log('success');
$timeout(function () {
$scope.results = response.data;
});
});
The revised code snippet differs slightly as there is no longer a need for test data and now the actual response data can be directly applied, eliminating the previous issue.