Angularjs表参数不起作用?

时间:2016-07-07 15:43:56

标签: javascript angularjs

我正在尝试第一次实现AngularJs NgTableParams我能够将数据填充到表中但是当我使用params.page()等方法抛出错误时,我遇到了问题ng-table

1-所以当我不使用params方法时,我能够获取表的数据但我看到错误$defer.resolve is not a function

2-当我使用params时,我收到错误can not read page its undefined

任何想法如何解决这些问题,我想要在下面的代码中设置我的表格选项?

ctrl.js

angular.module('loggingApp').controller('ditTableCtrl', function($scope, DitFactory, $filter, NgTableParams) {
    'use strict';
    $scope.logs = [];
    DitFactory.getLogs().then(function(response) {
        $scope.logs = response.data;
        console.log($scope.logs);
    });
    $scope.usersTable = new NgTableParams({
        page: 1,
        count: 10
    }, {
        total: $scope.logs.length,
        getData: function($defer, params) {
            $scope.data = $scope.logs.slice((params.page() - 1) * params.count(), params.page() * params.count());
            $defer.resolve($scope.data);
        }
    });
});

main.html中

  <table ng-table="usersTable" class="table table-striped">
        <tr ng-repeat="user in data">
            <td data-title="'Id'" >
                {{user.id}}
            </td>
            <td data-title="'First Name'" >
                {{user.first_name}}
            </td>
            <td data-title="'Last Name'" >
                {{user.last_name}}
            </td>
            <td data-title="'e-mail'" >
                {{user.email}}
            </td>
            <td data-title="'Country'">
                {{user.country}}
            </td>
            <td data-title="'IP'" >
                {{user.ip_address}}
            </td>
        </tr>
    </table>

1 个答案:

答案 0 :(得分:0)

我之前没见过$ defer。我想你想使用一个承诺,在这种情况下它将是$ q.defer(),延迟作为$ q promise服务中的方法。这些方面的东西:

var deferred = $q.defer();
deferred.resolve($scope.data);

首先阅读承诺,有几种资源。这只是一对: https://docs.angularjs.org/api/ng/service/ $ Q

http://fdietz.github.io/recipes-with-angular-js/consuming-external-services/deferred-and-promise.html