【发布时间】:2017-10-07 05:25:05
【问题描述】:
所以我一直在使用 angularjs 和 symfony 开发一个应用程序。我试图每 5000 毫秒重复检索一次数据,所以我使用“$timeout”功能从服务器获取实时数据检索。
这是我的控制器代码:
.controller('clientController', [ '$rootScope','$scope', 'Api' , 'toastr',
'$window', '$routeParams', '$filter' , '$timeout',
function($rootScope,$scope, Api ,toastr, $window, $routeParams, $filter ,
$timeout) {
$rootScope.myVar=true;
$scope.clients = [];
var retrieveItems = function () {
Api.getAllClients()
.then(function (result) {
console.log('result', result);
$scope.clients = result.data;
$timeout(retrieveItems, 5000);
}, function (error) {
console.log('error', error);
});
};
...
}])
这是 vue 代码:
<table class="table table-striped table-bordered" id="editable-datatable">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>tel</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="client in clients" id="1" class="gradeX">
<td>{{client.username}}</td>
<td>{{client.email}}</td>
<td>{{client.tel}}</td>
<td collapse=3>
<a style="border: 1px solid rgba(10, 6, 6, 0.25); padding: 4px;" ng-href="#!/client/{{client.id}}" ng-click="ClientDetailsController()">
<i class="icon-eye-open"></i> Details
</a>
<span style="margin-left: 10px"><a data-toggle="modal" href data
target="#exampleModal" ng-click="setID(client.id)" >
<i class="icon-remove-sign"></i> Delete
</a></span>
</td>
</tr>
</tbody>
</table>
我的问题是,每当我删除“retrieveItems”功能时,一切正常。我得到了所有的客户端,但是什么时候添加超时服务,这样我就可以每 5 秒刷新一次注册的客户端......客户端不会在 vue 上加载,但在错误部分没有错误。有人请告诉我问题到底是什么......任何其他建议可以使实时检索功能。提前致谢 。
【问题讨论】:
-
我认为你必须添加 $scope.$apply().
-
嗨!非常感谢您的回复。您能告诉我我应该把它放在哪里吗?
-
你的代码看起来不错,也许你总是得到相同的数据
标签: javascript angularjs