UI网格不会更新

时间:2016-05-06 19:41:24

标签: angularjs angular-ui-grid

我已经阅读了之前的所有问题,尝试了几种解决方案,但我的网格仍然无法更新。它正确加载所有数据。但是,如果我尝试添加数据,则网格不会更新。我有一个简单的测试按钮来添加数据,但是,警报显示但网格没有更新。

(function() {
  'use strict';

  angular
    .module('app')
    .controller('MainCtrl', MainCtrl);

  MainCtrl.$inject = ['$scope', '$state', 'Auth', '$modal', 'scrapeAPI', '$http',  'uiGridConstants', '$alert', 'recommendationsAPI', 'Upload'];

  function MainCtrl($scope, $state, Auth, $modal, uiGridConstants, scrapeAPI, $http, $alert, recommendationsAPI, Upload) {
    $scope.user = Auth.getCurrentUser();

    $scope.recommendation = {};
    $scope.recommendations = [];
    $scope.recommendationPostForm = true;
    $scope.busy = true;
    $scope.allData = [];

    //get data from server
    recommendationsAPI.getAllRecommendations()
      .then(function(data) {
        console.log('looks found ');
        console.log(data);
        $scope.recommendations = data.data;
        $scope.allData = data.data;
        // $scope.nextPage();
        $scope.busy = false;
      })
      .catch(function(err) {
        console.log('failed to get looks ' + err);
      });


    $scope.addData = function() {
    var n = $scope.recommendationgrid.data.length + 1;
    alert (n);
    $scope.recommendations.data.push ({
                "recommendation": "abc",
              });           
    };

    // Hier wordt de grid opgebouwd met all informatie. 
    $scope.recommendationgrid = {
    showGridFooter: true,
    showColumnFooter: false,
    enableFiltering: false,
    columnDefs: [
        { field: 'recommendation'},

    ],
    data: 'recommendations',
    onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
    }
};

  }
})();

2 个答案:

答案 0 :(得分:0)

我在这里看到您正在检查$scope.recommendationgrid.data的长度,然后将数据添加到$scope.recommendations.data

在此之下,您有$scope.recommendationgrid data指向字符串:

data: 'recommendations'

答案 1 :(得分:0)

您是否正确注入了angular .module('app', ['ui.grid']) .controller('MainCtrl', MainCtrl); 模块,如下所示?

data: 'recommendations',

此后检查... $scope.recommendationgrid = { showGridFooter: true, showColumnFooter: false, enableFiltering: false, columnDefs: [ { field: 'recommendation'}, ], data: $scope.recommendations.data, onRegisterApi: function(gridApi) { $scope.gridApi = gridApi; } }; 需要设置如下所示。

{{1}}