首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >监视在ng-repeat之外使用的ng-repeat对象

监视在ng-repeat之外使用的ng-repeat对象
EN

Stack Overflow用户
提问于 2015-10-11 05:35:34
回答 1查看 99关注 0票数 1

好了,我已经创建了一个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中。

示例

代码语言:javascript
复制
//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

代码语言:javascript
复制
<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-->
EN

回答 1

Stack Overflow用户

发布于 2015-10-11 05:54:58

检索新数据后,需要重置选定的对象。基本上,您只需要在新的对象数组中找到相应的记录,并再次将其设置为选中。

下面这样的代码应该能起到作用:

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33059206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档