好了,我已经创建了一个ng-repeat来获取由$http.get创建的所有用户。此get请求使用$interval每5秒更新一次,并在通过调用$scope.goInfo( data )单击时显示单个用户数据。此数据($scope.goInfo)在整个页面中用于显示用户数据,但它是由ng-repeat创建的(但并不总是在ng-repeat中使用)。如何让ng-repeat创建的这个数据obj在ng-repeat之外每隔5秒更新一次?我不能将$scope.goInfo()包装在$interval中。
示例
//CONTROLLER//
function liveFeed(){
$http.get('some URL').then(function (user) {
$scope.user = user.data;
console.log('user data is', $scope.user);
});
}
//Updates get req every five secs//
$interval(liveFeed, 5000);
//gets data obj from ng-repeat, needs to be updated every 5 secs.//
$scope.goInfo = function (data) {
$scope.name = data.name;
$scope.beats = data.beats;
}HTML
<table>
<th>First Name: John</th>
<th>Last Name:</th>
<tr ng-repeat="data in user" ng-click = "goInfo(data)">
<td>{{data.name}}<td>
</tr>
</table>
<span>{{beats}}</span><!--needs to update every 5 secs, outside of ng-repeat and be binded to the user that was clicked on-->发布于 2015-10-11 05:54:58
检索新数据后,需要重置选定的对象。基本上,您只需要在新的对象数组中找到相应的记录,并再次将其设置为选中。
下面这样的代码应该能起到作用:
function liveFeed() {
$http.get('some URL').then(function(user) {
$scope.user = user.data;
// Find the record that was selected before this update
if ($scope.selectedUser) {
$scope.selectedUser = $scope.user.filter(function(obj) {
return obj.name === $scope.selectedUser.name; // or compare by unique id
})[0];
}
});
}
// Updates get req every five secs
$interval(liveFeed, 5000);
// Gets data obj from ng-repeat, needs to be updated every 5 secs
$scope.goInfo = function(data) {
$scope.selectedUser = data;
}超文本标记语言将使用selectedUser
<table>
<tr>
<th>First Name: John</th>
<th>Beats:</th>
</tr>
<tr ng-repeat="data in user" ng-click="goInfo(data)">
<td>{{data.name}}<td>
<td>{{data.beats}}</td>
</tr>
</table>
<span>{{selectedUser.beats}}</span>https://stackoverflow.com/questions/33059206
复制相似问题