1

我有一个 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

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

4

2 回答 2

0

似乎ngGrid在传入options.data之前专门监视array.length。从这里复制:

$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});
  };                   
于 2013-10-23T08:43:13.807 回答
0

我将此作为问题添加到 ng-grids github 站点上。事实证明,其他人也遇到了同样的问题,并且有一个解决方案:(尽管您需要亲自动手并更改 ng-grid 代码。)

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

我猜它会进入3.0版

于 2013-10-23T10:29:48.573 回答