参数在 ng-table 的 getData 函数中未定义
Params undefined in ng-table's getData function
我 运行 使用 ng-table 遇到了一个问题,其中应传递到我的 getData 函数的参数未定义。我是 AngularJS 和 ng-table 的新手,因此我们将不胜感激。我已经通过直接调用下面代码中的 REST 调用来验证它们是否有效,所以问题出在我的 angular code/configuration.
无论如何,这是我的控制器的伪示例。实际代码在内部网上,所以我不能直接粘贴,所以请原谅转录中的错别字。使用 ng-table 1.0.0 和 angular 1.5.8:
myApp.controller('myCtrl', ['$scope', '$http', 'NgTableParams',
function($scope, $http, NgTableParams) {
$http.get('services/data/count').success(function(data) {
// this works fine
$scope.totalRows = data.rowCount;
});
$scope.tableParams = new NgTableParams({
page: 1
count: 20
}, {
total: $scope.totalRows,
getData: function($defer, params) {
// this line fails with params being undefined
$http.get('/services/data/' + params.page() + '/' + params.count()) {
.success(function(data) {
$scope.data = data;
$defer.resolve(data);
});
}
});
}]);
这里是 html 的相关部分:
<table ng-table="tableParams" class="table table-condensed table-striped">
<tr ng-repeat="row in data">
// row stuff here
</tr>
</table>
我在 getData http 调用之前插入了 console.log 语句,并且 params 打印为未定义。
$defer 变量(感谢 Jesse Amano!)具有我需要的值。我不确定为什么会这样,但我可以使用它。
抱歉,没想到我的评论会令人困惑。这是你的答案:
您输入 getData
键的函数被假定(由 NgTable API)接受一个参数,代表 params
。换句话说,getData
函数的第一个参数总是包含 params
个值,即使您将它命名为 $defer
。第二个参数总是未定义的(毕竟 API 只用一个参数调用它)即使你将它命名为 params
.
如果你想访问 $defer
(你似乎这样做了),我认为你应该将它注入你的控制器(将 '$defer'
添加到顶部的依赖项数组,然后将 $defer
添加到控制器函数的参数列表中 在同一位置 。)
看起来像这样:
myApp.controller('myCtrl', ['$scope', '$http', '$defer', 'NgTableParams',
function($scope, $http, $defer, NgTableParams) {
$http.get('services/data/count').success(function(data) {
$scope.totalRows = data.rowCount;
});
// ...
getData: function(params) {
$http.get('/services/data/' + params.page() + '/' + params.count()) {
.success(function(data) {
$scope.data = data;
$defer.resolve(data);
});
}
当我更新 ng-table 时,我也 运行 遇到了这个问题。您的代码应该适用于 1.0.0 版本之前的版本。 1.0.0-beta.9 是支持您的代码的最后一个版本。
在 1.0.0 change notes 中说:
getData 签名更改
提供给您的 getData 方法的 $defer 参数已被删除。相反你的
getData 方法应该 return 数组或解析为数组的承诺。
迁移
之前:
var tp = new NgTableParams({}, { getData: getData });
function getData($defer, params){
// snip
$defer.resolve(yourDataArray);
}
现在:
var tp = new NgTableParams({}, { getData: getData });
function getData(params){
// snip
return yourDataArrayOrPromise;
}
我 运行 使用 ng-table 遇到了一个问题,其中应传递到我的 getData 函数的参数未定义。我是 AngularJS 和 ng-table 的新手,因此我们将不胜感激。我已经通过直接调用下面代码中的 REST 调用来验证它们是否有效,所以问题出在我的 angular code/configuration.
无论如何,这是我的控制器的伪示例。实际代码在内部网上,所以我不能直接粘贴,所以请原谅转录中的错别字。使用 ng-table 1.0.0 和 angular 1.5.8:
myApp.controller('myCtrl', ['$scope', '$http', 'NgTableParams',
function($scope, $http, NgTableParams) {
$http.get('services/data/count').success(function(data) {
// this works fine
$scope.totalRows = data.rowCount;
});
$scope.tableParams = new NgTableParams({
page: 1
count: 20
}, {
total: $scope.totalRows,
getData: function($defer, params) {
// this line fails with params being undefined
$http.get('/services/data/' + params.page() + '/' + params.count()) {
.success(function(data) {
$scope.data = data;
$defer.resolve(data);
});
}
});
}]);
这里是 html 的相关部分:
<table ng-table="tableParams" class="table table-condensed table-striped">
<tr ng-repeat="row in data">
// row stuff here
</tr>
</table>
我在 getData http 调用之前插入了 console.log 语句,并且 params 打印为未定义。
$defer 变量(感谢 Jesse Amano!)具有我需要的值。我不确定为什么会这样,但我可以使用它。
抱歉,没想到我的评论会令人困惑。这是你的答案:
您输入 getData
键的函数被假定(由 NgTable API)接受一个参数,代表 params
。换句话说,getData
函数的第一个参数总是包含 params
个值,即使您将它命名为 $defer
。第二个参数总是未定义的(毕竟 API 只用一个参数调用它)即使你将它命名为 params
.
如果你想访问 $defer
(你似乎这样做了),我认为你应该将它注入你的控制器(将 '$defer'
添加到顶部的依赖项数组,然后将 $defer
添加到控制器函数的参数列表中 在同一位置 。)
看起来像这样:
myApp.controller('myCtrl', ['$scope', '$http', '$defer', 'NgTableParams',
function($scope, $http, $defer, NgTableParams) {
$http.get('services/data/count').success(function(data) {
$scope.totalRows = data.rowCount;
});
// ...
getData: function(params) {
$http.get('/services/data/' + params.page() + '/' + params.count()) {
.success(function(data) {
$scope.data = data;
$defer.resolve(data);
});
}
当我更新 ng-table 时,我也 运行 遇到了这个问题。您的代码应该适用于 1.0.0 版本之前的版本。 1.0.0-beta.9 是支持您的代码的最后一个版本。
在 1.0.0 change notes 中说:
getData 签名更改
提供给您的 getData 方法的 $defer 参数已被删除。相反你的 getData 方法应该 return 数组或解析为数组的承诺。
迁移
之前:
var tp = new NgTableParams({}, { getData: getData });
function getData($defer, params){
// snip
$defer.resolve(yourDataArray);
}
现在:
var tp = new NgTableParams({}, { getData: getData });
function getData(params){
// snip
return yourDataArrayOrPromise;
}