小编典典

AngularJS ngRepeat更新模型

angularjs

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
  });
});

如您所见,我正在$scope.foo定期更新阵列。但是,我的观点是这样构造的:

<tr ng-repeat="item in foo">
  ...
</tr>

问题是,当我更新时foo,它不会用新数据重新渲染我的表。

我将如何解决这个问题?


阅读 286

收藏
2020-07-04

共1个答案

小编典典

您是否将更新包入

$scope.$apply(function() {
  // update goes here
});

?这应该可以解决问题。

2020-07-04