ng-grid gridData有时只进行refeshing

时间:2013-10-22 14:21:09

标签: angularjs ng-grid

我有一个ng-grid,我试图填写一些搜索结果。 只要结果具有不同的行数,查询就会起作用... 如果查询的行数与上一个查询的行数相同,则结果将不会显示。

    // after a rest call 
    // we have the result
    // #1 Clean gridData and local array
      $scope.gridData.length = 0;    
      _labels.length = 0;

    // #2 feed the grid 
    // these two functions add rows to the grid + data to the rows (and my local array) 
            addLabels(result.Labels);
            addPhrases(result.Phrases);


    // Second version attempting to call $scope.$apply
    // does not work either

      $scope.gridData.length = 0;
        if (!$scope.$$phase) {
            $scope.$apply();
        }

        _labels.length = 0;
        addLabels(result.Labels);
        addPhrases(result.Phrases);
        if (!$scope.$$phase) {
            $scope.$apply();
        }

这是一个演示这个的plunker: http://plnkr.co/edit/gSFtuL?p=preview

请注意,如果您再推一个(或少一个)项目,网格会刷新

2 个答案:

答案 0 :(得分:0)

似乎ngGrid在传递options.data之前专门监视array.length。复制自here

$scope.$parent.$watch(options.data, dataWatcher);
$scope.$parent.$watch(options.data + '.length', function() {
    dataWatcher($scope.$eval(options.data));
});

我无法解释原因,但如果我分配一个空数组而不是重置数组长度,它对我有用。像这样:

  $scope.changeData = function() {
    /* $scope.myData.length =0; -- this doesn't work */
    $scope.myData = [];
    $scope.myData.push({name: "aMoroni", age: 51});
    $scope.myData.push({name: "bMoroni", age: 52});
    $scope.myData.push({name: "cMoroni", age: 53});
    $scope.myData.push({name: "dMoroni", age: 54});
    $scope.myData.push({name: "eMoroni", age: 55});
  };                   

答案 1 :(得分:0)

我在ng-grid github网站上添加了这个问题。事实证明,其他人遇到了同样的问题,并且有一个解决方案: (你需要亲自动手并改变ng-grid代码。)

https://github.com/angular-ui/ng-grid/issues/632

我猜它会进入3.0版本