var Group = require('../models/group/GroupAlm.js');
var fs = require('fs');
module.exports = function(groupalm) {
groupalm.get('/groupalm/:nim', function(req, res){
var getOne = req.params.nim;
Group.find({ nim:getOne}).select('postings').exec(function(err, grooup){
if (err){
res.json({ success: false, message: 'Not Found'});
}else{
res.json({ success:true, grooup:grooup});
}
})
});return groupalm;};
和2.致电获取api
var appFactory=angular.module('app.factory',[]);
appFactory.factory('AllGrp',function($http){
var allNotFactory = {};
allNotFactory.getAllAlmGrp = function(nim) {
return $http.get('/mod_mygroup/groupalm/' + nim);
};
return allNotFactory;
});
3是我的控制者
app.controller('appCtrl', ['$scope','AllGrp', function ($scope,AllGrp) {
function getGroupByPKNIMalm() {
var nim = 11037050017;
AllGrp.getAllAlmGrp(nim).then(function(cek){
if (cek.data.success){
for (var i = 0; i < cek.data.grooup.length; i++){
$scope.grp = cek.data.grooup[i].postings;
console.log('data',$scope.grp);
}
}else{ console.log('err');}
});
}; getGroupByPKNIMalm();
}]);
更新撰写HTML 这个ng-repeat用于显示数据
<li class="list-group" role="presentation" ng-repeat="groups in grp | orderBy:'created_at':true ">
<div data-role="container">
<div data-role="content">
<a class="list-group-item" href="javascript:void(0)" role="menuitem">
<div class="media">
<div class="media-left padding-right-10">
<i class="icon md-receipt bg-red-600 white icon-circle" aria-hidden="true"></i>
</div>
<div class="media-body">
<h6 class="media-heading">{{groups.nim}}</h6>
<h6 class="media-meta">{{groups.post}}</h6>
</div>
</div>
</a>
/ END UPDATE TEXT HTML /
步骤1-3中的okey成功从api获取数据,这是获取/查找后的数据:
{
"success": true,
"grooup": [
{
"_id": "59eeb992a6ddc00f1037c1cf",
"postings": [
{
"nim": 1137050017,
"nama": "ADRIYANA PUTRA PRATAMA",
"post": "hay salam kenal",
"created_at": "2017-10-24T03:55:17.004Z"
},
{
"nim": 1111111,
"nama": "Fakih Nuzli",
"post": "hay salam kenal",
"created_at": "2017-10-24T04:01:14.954Z"
}
]
},
{
"_id": "59ef26cc0cfb2618d047cc6f",
"postings": [
{
"nim": 1137050017,
"nama": "ADRIYANA PUTRA PRATAMA",
"post": "selata datang",
"created_at": "2017-10-24T21:27:06.091Z"
},
{
"nim": 1111111,
"nama": "Fakih Nuzli",
"post": "ya thanx",
"created_at": "2017-10-24T21:27:29.384Z"
},
]
},
{
"_id": "59e53fcd1e85242ea417832e",
"postings": [
{
"nim": 1111111,
"nama": "Fakih Nuzli",
"post": "hay",
"created_at": "2017-10-24T11:19:44.318Z"
}
],
}
]}
问题是在步骤3中,我在控制器中循环数据,用于显示所有数据postings
,再回头看看步骤3,有一个代码用于循环数据,如此代码循环
for (var i = 0; i < cek.data.grooup.length; i++){
$scope.grp = cek.data.grooup[i].postings;
console.log('data',$scope.grp);
}
成功编写代码以显示数据postings
我查看console.log
,但在ng-repeat="groups in grp"
之后我尝试显示所有posting
{{groups.nim}} and {{groups.post}}
为什么他只是在最后一个ID 59ef26cc0cfb2618d047cc6f
显示数据。
那么如何显示所有数据贴子?在我循环后,我从api Group.find({nim})
获取数据。
谢谢;)
答案 0 :(得分:1)
您需要合并每个帖子中的数组,如下所示,
AVFoundation
<强>样本强>
$scope.grp.push( ...$scope.result.grooup[i].postings);
&#13;
var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.grp=[];
$scope.result = {
"success": true,
"grooup": [
{
"_id": "59eeb992a6ddc00f1037c1cf",
"postings": [
{
"nim": 1137050017,
"nama": "ADRIYANA PUTRA PRATAMA",
"post": "hay salam kenal",
"created_at": "2017-10-24T03:55:17.004Z"
},
{
"nim": 1111111,
"nama": "Fakih Nuzli",
"post": "hay salam kenal",
"created_at": "2017-10-24T04:01:14.954Z"
}
]
},
{
"_id": "59ef26cc0cfb2618d047cc6f",
"postings": [
{
"nim": 1137050017,
"nama": "ADRIYANA PUTRA PRATAMA",
"post": "selata datang",
"created_at": "2017-10-24T21:27:06.091Z"
},
{
"nim": 1111111,
"nama": "Fakih Nuzli",
"post": "ya thanx",
"created_at": "2017-10-24T21:27:29.384Z"
}
]
},
{
"_id": "59e53fcd1e85242ea417832e",
"postings": [
{
"nim": 1111111,
"nama": "Fakih Nuzli",
"post": "hay",
"created_at": "2017-10-24T11:19:44.318Z"
}
]
}
]};
for (var i = 0; i < $scope.result.grooup.length; i++){
$scope.grp.push( ...$scope.result.grooup[i].postings);
}
});
&#13;