angularjs ng-table / ng-repeat行未加载

时间:2017-02-22 11:27:26

标签: javascript html angularjs angularjs-ng-repeat ngtable

我正在尝试按照教程Here使用ng-Table。如果我从Git下载整个示例然后它加载正常,但尝试按照自己最简单的示例然后列标题和过滤器加载,但没有行?如果我手动在HTML中添加另一行,它确实会出现,因此问题似乎与数据绑定或ng-repeat有关。控制台中没有显示错误。

enter image description here

示例代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-resource.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.js"></script>
    <link rel="stylesheet" ; href="https://unpkg.com/ng-table@4.0.0/bundles/ng-table.css">
    <script src="https://unpkg.com/ng-table@4.0.0/bundles/ng-table.js"></script>
</head>
<body>

    <div ng-app="MyApp" ng-controller="MyCtrl">

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


    <script>
        var app = angular.module("MyApp", ["ngTable"]);

        app.controller('MyCtrl', function ($scope, NgTableParams) {
            var self = this;
            var data = [{ name: "Moroni", age: 50 } /*,*/];
            self.tableParams = new NgTableParams({}, { dataset: data });
        });

    </script>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

您尚未指定控制器参考。

将代码更改为<div ng-app="MyApp" ng-controller="MyCtrl as vm">,它会按预期运行。

工作示例:http://codepen.io/DeividasK/pen/qrWqey?editors=1010

答案 1 :(得分:0)

您没有在范围内分配对象

尝试以下代码而不是代码

    app.controller('MyCtrl', function ($scope, NgTableParams) {
        var self = this;
         self.data  = [{ name: "Moroni", age: 50 } /*,*/];
        self.tableParams = new NgTableParams({}, { dataset:  self.data });
    });

答案 2 :(得分:0)

这是ng表,问题是你使用控制器作为vm,只需删除vm或将控制器作为vm并且它可以工作。

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