【发布时间】:2014-08-11 04:23:04
【问题描述】:
我在执行以下顺序的一个步骤时遇到问题:
- 将数据发布到休息服务器并获得完整的记录作为回报。
- 用新记录更新网页。
我有一个可用的 $resource 并使用以下命令在控制器中发布我的帖子。
$scope.update = function(newRecord) {
//use resource called apiClient to POST a record to API server
apiClient.save(newRecord).$promise.then(function(response) {
//attempt to add new complete record to list on the web page
$scope.existingRecords.push(response.data);
});
};
但是我的 ng-repeat 列表不会更新以反映新条目。我可以确认数组 $scope.existingRecords(它驱动我的 ng-repeat)正在获取新记录,但在 DOM 中没有任何乐趣。
我听说过一些选择,但我没有运气......
$scope.update = function(newRecord) {
//use resource called apiClient to POST a record to API server
apiClient.save(newRecord).$promise.then(function(response) {
//attempt to add new complete record to list on the web page
$scope.existingRecords.push(response.data);
//let's try to force an update
$scope.$apply();
});
};
给我一个错误....
错误:[$rootScope:inprog] $digest 已经在进行中
修改以绕过摘要错误...
....
$timeout(function() {
$scope.$apply();
});
....
运行无错误,但也不更新 ng-repeat。
更新 新创建的记录添加:
$scope.existingRecords.push(response.data);
Firebug 显示已有记录和新记录的方式略有不同:
Resource { id="2", name="existing-record", more...}
Object { id="41", name="just posted via ajax", more...}
不确定这是否重要,也许我以错误的方式将新记录添加到模型中?
【问题讨论】:
-
你找到解决方案了吗?
-
恐怕我没有。不久之后,我放弃了 Angular,喝了 Ember kool-aid。
-
LOl。好,谢谢。我的直接问题似乎是缓存刷新问题。 ng-repeat 在彻底清除浏览器和服务器缓存后正确呈现。
标签: angularjs angularjs-ng-repeat angularjs-resource