-->

Angular ng-table not loading dataset?

2019-07-14 08:24发布

问题:

I'm trying to use the ng-table directive. Having a really hard time just getting the demo code to work properly in my local project.

http://ng-table.com

In my controller, I set up my data.

    var dataset = [
        {name: "Moroni50", age: 50},
        {name: "Moroni49", age: 49},
        {name: "Moroni48", age: 48},
        {name: "Moroni47", age: 47},
        {name: "Moroni46", age: 46},
    ];

    $scope.tableParams = new NgTableParams({}, {dataset: dataset});

When I console.log($scope.tableParams) it seems the object has been created successfully. However the it shows data is an empty array with 0 length.

The render code then just shows the table header with no data.

<table ng-table="tableParams" class="table" show-filter="true">
    <tr ng-repeat="user in $data">
        <td title="'Name'" sortable="'name'">{{ user.name }}</td>
        <td title="'Age'" sortable="'age'">{{ user.age }}</td>
    </tr>
</table>

No idea what's going on, is there something simple I'm missing?

回答1:

I think the documentation may be out of date. I got it working changing "dataset" to "data", like this (using v0.8.3):

$scope.tableParams = new NgTableParams({}, {data: dataset});

You can also add a count (otherwise it just displays one item per page).

$scope.tableParams = new NgTableParams({
    count: 10 
}, {
    data: dataset
});

Working demo: http://plnkr.co/edit/Sbaqgzo6QT3sjxEduRFO?p=preview



回答2:

As Rob said, it looks like the documentation is referencing the latest 1.x beta versions. At 1.0.0-beta.4 they changed settings().data to settings().dataset. So, if you're using an older version use data.

https://github.com/esvit/ng-table/releases?after=1.0.0-beta.5