当列表动态更新时,ng-repeat不会复制数据

时间:2015-06-29 04:34:34

标签: angularjs html5 dynamic ng-repeat

我正在动态地向数组添加数据并尝试使用ng-repeat复制div。

如果最初添加数据,它会正确呈现,但如果我们稍后动态添加数据则不会。

var app = angular.module('game',[]);
  app.controller('myCtrl', function($scope) {

  var json = [];
  $scope.setlobbyData = function(obj){
      $scope.json = obj;
      $scope.lobbyData = $scope.json;
  };
  });

HTML

<div ng-controller="myCtrl">
<div ng-repeat="i in lobbyData">some data</div>
</div>

JS

var ssr = {
    "0": {
        "tradeSpread": 0,
        "minBuyIn": 10,
        "minPlayers": 2,

    },
    "1": {
        "tradeSpread": 0,
        "minBuyIn": 10,
        "minPlayers": 2,

    }
};
angular.element($("#foreignExchangeMain")).scope().setlobbyData(ssr);

3 个答案:

答案 0 :(得分:0)

我认为解决方案可能是调用

$scope.$apply();

见这里: How can I tell AngularJS to "refresh"

答案 1 :(得分:0)

请参阅此文章以更新外部角度的范围数据:

AngularJS access scope from outside js function

答案 2 :(得分:0)

使用$ apply刷新

 var scope = angular.element($("#foreignExchangeMain")).scope();
        scope.$apply(function(){
            scope.json = ssr;
            scope.setlobbyData(ssr)

        });