Angular 1.5 范围变量在 PouchDB 响应函数中不起作用
Angular 1.5 Scope Variable not working from within PouchDB response function
所以我得到了以下代码...
testApp.controller(...) {
$scope.results = [];
$scope.hasData = true;
$scope.results.push({
"name": "test"
}); // WORKS
db.get('table_people').then(function(response) {
console.log('success');
$scope.results.push({
"name": "test"
});
}); // this DOESN'T WORK even though the "success" message is printed...
});
从评论中可以看出,数组的第一个 push
有效,但后一个无效。可以使用 {{ results }}
在 Angular 模板中打印出前一个,但后一个 returns 是一个空数组。
编辑: 通过使用 $timeout
找到了解决方案,因为 摘要循环 不是 运行 但感觉有点像破解的解决方案。
编辑:解决方案...
db.get('table_people').then(function (response) {
console.log('success');
$timeout(function () {
$scope.results = response.data;
});
});
解决方案代码略有不同,由于代码运行,我不再需要测试数据,可以直接应用响应数据。
您缺少 $digest
循环标记。在将数据推送到 $scope.results
后执行 $scope.$digest()
应该可以解决问题。在这种情况下使用 $timeout
有点矫枉过正(以及要注入的额外服务)。
所以我得到了以下代码...
testApp.controller(...) {
$scope.results = [];
$scope.hasData = true;
$scope.results.push({
"name": "test"
}); // WORKS
db.get('table_people').then(function(response) {
console.log('success');
$scope.results.push({
"name": "test"
});
}); // this DOESN'T WORK even though the "success" message is printed...
});
从评论中可以看出,数组的第一个 push
有效,但后一个无效。可以使用 {{ results }}
在 Angular 模板中打印出前一个,但后一个 returns 是一个空数组。
编辑: 通过使用 $timeout
找到了解决方案,因为 摘要循环 不是 运行 但感觉有点像破解的解决方案。
编辑:解决方案...
db.get('table_people').then(function (response) {
console.log('success');
$timeout(function () {
$scope.results = response.data;
});
});
解决方案代码略有不同,由于代码运行,我不再需要测试数据,可以直接应用响应数据。
您缺少 $digest
循环标记。在将数据推送到 $scope.results
后执行 $scope.$digest()
应该可以解决问题。在这种情况下使用 $timeout
有点矫枉过正(以及要注入的额外服务)。