角度ui-grid选项对象的问题

时间:2015-10-09 07:00:06

标签: angularjs angular-ui-grid

我正在努力将数据推送到我在我的角度应用程序中设置的ui-grid。

这是我做的:

<div ng-controller="MainCtrl">
  <div id="grid1" ui-grid="gridOptions" class="grid"></div>
</div>

app.controller('MainCtrl', ['$scope', function ($scope) {

$scope.gridOptions.data = [
    {
        "firstName": "Cox",
        "lastName": "Carney",
        "company": "Enormo",
        "employed": true
    },
    {
        "firstName": "Lorraine",
        "lastName": "Wise",
        "company": "Comveyer",
        "employed": false
    },
    {
        "firstName": "Nancy",
        "lastName": "Waters",
        "company": "Fuelton",
        "employed": false
    }
];

}]);

但是我的网格中没有显示任何数据。谁知道为什么?

Plunker - http://plnkr.co/edit/tCBB3nfsLtFhfIgKNk47

1 个答案:

答案 0 :(得分:1)

问题出在你的gridOptions对象中,你必须在分配数据之前定义它:

$scope.gridOptions = {data: []};
$scope.gridOptions.data = [
    {
        "firstName": "Cox",
        "lastName": "Carney",
        "company": "Enormo",
        "employed": true
    },
    {
        "firstName": "Lorraine",
        "lastName": "Wise",
        "company": "Comveyer",
        "employed": false
    },
    {
        "firstName": "Nancy",
        "lastName": "Waters",
        "company": "Fuelton",
        "employed": false
    }
];

问题是gridOptions没有定义,所以它在控制台中抛出了这个错误:TypeError: Cannot set property 'data' of undefined