AngularJS ngRepeat更新模型

if *_*one 13 javascript angularjs angularjs-ng-repeat

ng-repeat用来渲染一个html table.我的控制器看起来像这样:

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
  });
});
Run Code Online (Sandbox Code Playgroud)

如您所见,我$scope.foo定期更新阵列.但是,我的视图构造如下:

<tr ng-repeat="item in foo">
  ...
</tr>
Run Code Online (Sandbox Code Playgroud)

问题是,当我更新时foo,它不会使用新数据重新呈现我的表.

我该如何解决这个问题?

lex*_*x82 25

你把更新包装好吗?

$scope.$apply(function() {
  // update goes here
});
Run Code Online (Sandbox Code Playgroud)

?这应该可以解决问题.

  • 此外,您可以在更改后放置$ scope.$ apply(). (4认同)