【问题标题】:ng-grid gridData only refeshing sometimesng-grid gridData 有时只刷新
【发布时间】:2013-10-31 11:29:03
【问题描述】:

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

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

【问题讨论】:

  • 你定义了gripOptions吗?你能展示它和你的标记吗?

标签: angularjs ng-grid


【解决方案1】:

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

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

我猜它会进入 3.0 版

【讨论】:

【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2015-12-12
    相关资源
    最近更新 更多