[英]AngularJS ngRepeat update model
I use ng-repeat
to render a html table
. 我使用
ng-repeat
来渲染一个html table
。 My controller looks like so: 我的控制器看起来像这样:
app.controller("fooCtrl", function($scope, WebSocket) {
$scope.foo = [ ... ];
WebSocket.start(function(data) {
// this is a callback on websocket.onmessage
// here is a for loop iterating through $scope.foo objects and updating them
});
});
As you can see, I am updating the $scope.foo
array periodically. 如您所见,我定期更新
$scope.foo
数组。 However, my view is constructed like so: 但是,我的视图构造如下:
<tr ng-repeat="item in foo">
...
</tr>
The problem is, when I update foo
, it doesn't re-render my table with new data. 问题是,当我更新
foo
,它不会使用新数据重新渲染我的表。
How would I go about this problem? 我该如何解决这个问题?
Did you wrap the update in 你把更新包装好吗?
$scope.$apply(function() {
// update goes here
});
? ? This should solve the problem.
这应该可以解决问题。
You probably need to call $apply()
to force angular to do a digest cycle when you update the list, like: 您可能需要调用
$apply()
来强制angular在更新列表时执行摘要循环,例如:
WebSocket.start(function(data) {
$scope.$apply(function(){
/* your stuff goes here*/
});
});
Following code work for me, Whenever you get message from socket you just need to bind your code inside $scope.$apply() function 以下代码为我工作,每当你从套接字获得消息时,你只需要在$ scope。$ apply()函数中绑定你的代码
socket.on = function (e) {
$scope.$apply(function () {
// update you UI over here
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.