ngTable:无法读取属性“页面”?

时间:2016-08-10 02:32:13

标签: javascript angularjs pagination ngtable

我正在尝试实现ngTable来显示来自其余调用的json。在我的工厂js中,我为http get请求定义了获取所有记录的方法。

> utils::contrib.url
function (repos, type = getOption("pkgType")) 
{
    type <- resolvePkgType(type)
    if (is.null(repos)) 
        return(NULL)
    if ("@CRAN@" %in% repos && interactive()) {
        cat(gettext("--- Please select a CRAN mirror for use in this session ---"), 
            "\n", sep = "")
        flush.console()
        chooseCRANmirror()
        m <- match("@CRAN@", repos)
        nm <- names(repos)
        repos[m] <- getOption("repos")["CRAN"]
        if (is.null(nm)) 
            nm <- rep("", length(repos))
        nm[m] <- "CRAN"
        names(repos) <- nm
    }
    if ("@CRAN@" %in% repos) 
        stop("trying to use CRAN without setting a mirror")
    ver <- paste(R.version$major, strsplit(R.version$minor, ".", 
        fixed = TRUE)[[1L]][1L], sep = ".")
    mac.path <- "macosx"
    if (substr(type, 1L, 11L) == "mac.binary.") {
        mac.path <- paste(mac.path, substring(type, 12L), sep = "/")
        type <- "mac.binary"
    }
    res <- switch(type, source = paste(gsub("/$", "", repos), 
        "src", "contrib", sep = "/"), mac.binary = paste(gsub("/$", 
        "", repos), "bin", mac.path, "contrib", ver, sep = "/"), 
        win.binary = paste(gsub("/$", "", repos), "bin", "windows", 
            "contrib", ver, sep = "/"))
    res
}
<bytecode: 0x0000000007708e90>
<environment: namespace:utils>

ngTable在我的控制器js

中设置
ristoreApp.factory("fmFactory", ['$http', '$window',
    function ($http, $window) {
        var service = {};

        service.getAll = function () {
            var url = SERVER + "/ristore/foundation/";
            return $http({
                headers: {'Authorization': 'Bearer ' + $window.localStorage.getItem("access_token")},
                url: url,
                method: 'GET',
                crossOrigin: true
            })
        }

        return service;
    }]);

没什么好看的,只是简单的分页,每页10条记录。但是,我在ristoreApp.controller("fmCtrl", ['$scope', 'fmFactory', 'NgTableParams', function($scope, fmFactory, NgTableParams) { $scope.selection = '0'; $scope.reports = []; $scope.fmSearch = function () { if ($scope.selection == '0') { fmFactory.getAll().success(function (data) { $scope.reports = data; $scope.tableParams = new NgTableParams({ page: 1, // show first page count: 10 // count per page }, { total: $scope.reports.length, // length of data getData: function ($defer, params) { $defer.resolve($scope.reports.slice((params.page() - 1) * params.count(), params.page() * params.count())); } }); }) } } }] ) 中的方法TypeError: Cannot read property 'page' of undefined收到了错误params.page()。这很奇怪。显然,“页面”在$defer.resolve()的参数部分中定义。为什么抱怨它没有定义?

编辑:

根据Sergii的答案link,我删除了$ defer并将我的控制器js更改为以下内容:

NgTableParams

但是没有显示任何东西,只有一堆线条。 http调用url已经过测试,并使用rest api tester返回正确的promise。

1 个答案:

答案 0 :(得分:3)

正如我在评论中写的那样 params未定义,但异常被包装\处理角度,这是部分正确的异常信息的原因。

我认为出现此问题是因为您现在正在使用最新的 ng-table-1.0.0 库。如果您导航到Angular ngTableDynamic example:server-side listAngular ngTable example:server-side list,请注意加载数据的API已更改。

  getData: function(params) {
    // ajax request to api
    return yourFactory.get(params.url()).$promise.then(function(data) {
      params.total(data.inlineCount);
      return data.results;
    });
  }

在您的参数 $ defer 中也有不同的对象(对象是参数)。如果您尝试应用提供的解决方案,请确保您更正了正确的参数:

params.url() - should be pagination filter like {page: 0, size: 10}
data.inlineCount - total elements on server side
data.results - data list from server side

我希望我的调查不仅有助于我解决这个问题。