我现在使用 AngularJS 一段时间了,它的工作效果很好。然而最近我对数据集有一些思考问题。
假设我有一系列项目:
[
{ id: 1, votes: 10, detailInformation: 'Interesting #1' },
{ id: 2, votes: 12, detailInformation: 'Interesting #2' }
]
我设置了一个 $timer,每 60 秒从 REST Web 服务中检索一次此信息,以便为用户提供最新信息。
这些项目以以下格式向用户显示:
| ID: {{item.id}} Votes: {{item.votes}} |
| <span data-ng-click="item.showInfo = true">Click here to view detail info</span> |
| <span data-ng-show="item.showInfo == true">{{item.detailInformation}}</span> |
此示例工作正常,但当计时器检索新内容时,它会丢弃 View 状态 (item.showInfo = true)。针对此类情况的最佳实践是什么?如何将这种仅限客户端的状态与需要更新的服务器信息分开?
最佳答案
在您的 Controller 中,您需要将 showInfo 添加为单独的对象,并且不包含在来自服务器的响应对象中,因此在您的 Controller 中如下所示:
app.controller('test', ['$scope','urService',function($scope,urService){
$scope.showInfo = true;
urService.getData(function(data){
//update data in the another scope object
}
}]);
关于javascript - 如何在 AngularJS 中处理实时数据和查看状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25293061/