【发布时间】:2015-05-23 01:53:23
【问题描述】:
我正在关注此处描述的 ng-table 示例:http://bazalt-cms.com/ng-table/example/1 using mongoose-middleware https://github.com/PlayNetwork/mongoose-middleware。我的代码如下所示。 列表-insumos.client.view.html:
<section data-ng-controller="InsumosController" data-ng-init="find()">
<div class="page-header">
<h1>Insumos</h1>
</div>
<table ng-table="tableParams">
<tr ng-repeat="insumo in $data">
<td data-title="'Nombre'"> {{insumo.name}}</td>
....
insumos.server.controller.js:
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors.server.controller'),
Insumo = mongoose.model('Insumo'),
_ = require('lodash');
.....
/**
* List of Insumos
*/
exports.list = function(req , res){
var count = req.query.count || 5;
var page = req.query || 1 ;
var options = {
filters : {
mandatory : { contains : req.query.filter }
},
sort : {
asc : '_id',
},
start: (page-1)*count,
count: count
};
Insumo
.find()
.keyword(options)
.filter(options)
.order(options)
.page(options, function (err, insumos) {
if (!err) {
//console.log(insumos.results); //this gives me the array I was looking for
** res.jsonp(insumos); ** // the good stuff in insumos.results but I need to pass this in order to make the client controller take it as an object and use it for pagination in insumos.client.controller
} else { console.log(err); }
});
};
....
insumos.client.controller.js:
'use strict';
// Insumos controller
angular.module('insumos').controller('InsumosController', ['$scope', '$stateParams', '$location', 'Authentication', 'Insumos', **'ngTableParams' ** , '$filter', function($scope, $stateParams, $location, Authentication, Insumos, **ngTableParams**, $filter) {
$scope.authentication = Authentication;
var params = {
page: 1, // show first page
count: 5, // count per page
};
var settings = {
total: 0, // length of data
getData: function($defer, params) {
Insumos.get(params.url(), function(response){
params.total(response.total);
$defer.resolve(response.results); //creates an obj $data with the api results ("results" type is object, althought it should be Array... it is an object that contains 5 arrays)
});
}};
$scope.tableParams = new **ngTableParams**(params, settings); //I get a warning here saying I need to make this uppercase, but I think it's cool to leave it like that
.....
现在...我正在获取包含 5 项的清单(这很好)。当我单击表格操作按钮以显示 10 个项目时,它完美运行,它显示了前十个项目(现有项目总数:13)。到目前为止一切都很好。我点击“下一页”,没有任何反应。我检查了控制台,它说:
Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object
http://errors.angularjs.org/1.2.28/$resource/badcfg?p0=array&p1=object
所以我 console.log (typeof(response.results) +' - --- - '+ response.results) 我得到了
object - --- - [object Object],[object Object],[object Object],[object Object],[object Object]
所以...我有点卡在这里...我应该做一个 for 并将每个结果推送到 $defer.resolve 吗?我尝试了所有关于将对象转换为数组的技巧,但没有改变任何东西 有谁知道它应该收到什么?
仍然......列表被填满......前五个项目。我检查了“网络”选项卡,并查看了每次单击“下一页”按钮时发生的情况 似乎所有页面都有相同的前 5 个项目。same 前 5 个项目。无论我在哪个页面,它总是从第 0 项开始。
options: {filters: {mandatory: {}}, sort: {asc: "_id"}, start: 0, count: "5"}
count: "5"
filters: {mandatory: {}}
sort: {asc: "_id"}
start: 0
results: [{_id: "550763b325a55de01172d182", user: "5507633625a55de01172d180", __v: 0,…},…]
0: {_id: "550763b325a55de01172d182", user: "5507633625a55de01172d180", __v: 0,…}
1: {_id: "550763ba25a55de01172d183", user: "5507633625a55de01172d180", __v: 0,…}
2: {_id: "550832c26a3755ec289ecabf", user: "5507633625a55de01172d180", __v: 0,…}
3: {_id: "55087480332393bb3bb11ca3", user: "5507633625a55de01172d180", __v: 0,…}
4: {_id: "550878d9a3b14ece3d112b6d", user: "5507633625a55de01172d180", __v: 0,…}
total: 13
无论我设置哪个计数或哪个页面,它总是从第 0 项开始
insumos?count=10&page=1 - GET 304 Not Modified
insumos?count=10&page=2 - GET 304 Not Modified
insumos?count=5&page=1 - GET 304 Not Modified
insumos?count=5&page=2 - GET 304 Not Modified
insumos?count=5&page=3 - GET 304 Not Modified
所以问题是:
1-为什么分页没有改变列表中的项目?我怎样才能让它工作? 2-为什么是“预期和数组但有一个对象”错误?我怎样才能摆脱它?
一百万提前感谢任何阅读并花时间回复的人
【问题讨论】:
标签: angularjs pagination mongoose meanjs