Javascript 如何在套接字响应后更改ng repeat变量
嗨,伙计们,我有一个简单的角度:Javascript 如何在套接字响应后更改ng repeat变量,javascript,angularjs,Javascript,Angularjs,嗨,伙计们,我有一个简单的角度: app.controller('FriendsController', ['$scope', 'Page', 'Title', '$http', function($scope, Page, Title, $http) { $http.get('/friends') .success(function(data) { $scope.friends = data.data; }) socket.on('friendslist', func
app.controller('FriendsController', ['$scope', 'Page', 'Title', '$http', function($scope, Page, Title, $http) {
$http.get('/friends')
.success(function(data) {
$scope.friends = data.data;
})
socket.on('friendslist', function(resp) {
// Socket was emitted that a user was updated, they are now either Offline or Online
// So find the proper element and update them to the new value
if (resp.verb === "updated") {
for (var i = 0; i < $scope.friends; i++) {
if ($scope.friends[i].id === resp.id) {
$scope.friends[i].online = resp.data.online;
}
}
//var $friendEl = $('i').find('[data-id="1"]').first();
//console.log($friendEl);
//
}
});
}]);
app.controller('FriendsController',['$scope','Page','Title','$http',函数($scope,Page,Title,$http){
$http.get(“/friends”)
.成功(功能(数据){
$scope.friends=data.data;
})
socket.on('friendslist',函数(resp){
//当用户更新时发出套接字,用户现在处于脱机或联机状态
//因此,找到合适的元素并将其更新为新值
如果(分别动词==“已更新”){
对于(变量i=0;i<$scope.friends;i++){
if($scope.friends[i].id==resp.id){
$scope.friends[i].online=resp.data.online;
}
}
//var$friendEl=$('i').find('data id=“1”]').first();
//console.log($friendEl);
//
}
});
}]);
在发出套接字之后,我将数据发送到客户端,但是如果I
console.log(resp)
返回所需的数据,它似乎不会更新值,但是当Iconsole.log($scope.friends[I])时
在它更新之后,它仍然说它初始化时使用的任何东西,比如for循环,都找不到它并更新它,但我知道它是这样的。任何信息都将非常感谢。forloop中的$scope.friends.length不是应该是吗?我认为您可能需要在for循环中添加.length
到$scope.friends
,这样它将读取for(var I=0;I<$scope.friends.length;I++){
添加$scope.$apply()
告知范围已更改。另请参阅(和其他几个问题)