我想收集过去的限制聊天,但这会打印出所有的聊天记录。
<div ng-repeat="(key, value) in chats|limitTo:10">
<a href="" ng-click="ind(key, value);$event.preventDefault()" class="list-group-item">
<span class="badge">{{value.time}}</span>
<i class="fa fa-fw fa-calendar"></i> {{value.partner}}
</a>
</div>
这是控制器中的代码。
$scope.chats = null;
var chats = {}
var id = $store.get('doctor_id');
var d = new Date();
doctorFactory.getChatRooms(id).then(function(x){
for (var i in x){
chats[i] = { partner: x[i] }
}
$scope.chats = dateService.doEquation(chats, d);
console.log($scope.chats);
}).catch(function(err){
console.log(err);
});
console.log($scope.chats)
打印出一系列对象:
2016021085524: Object
clock: "10:15"
date: "02/10/2016"
partner: "Magruder_Douglas"
time: "0 years ago"
2016021085622: Object
clock: "10:21"
date: "02/10/2016"
partner: "Magruder_Douglas"
time: "0 years ago"
答案 0 :(得分:2)
limit不适用于对象,因为您需要实现自己的自定义过滤器:
app.filter('objLimitTo', [function(){
return function(obj, limit){
var keys = Object.keys(obj);
if(keys.length < 1) return [];
var ret = new Object();
var count = 0;
angular.forEach(keys, function(key, arrayIndex){
if(count >= limit) return false;
ret[key] = obj[key];
count++;
});
return ret;
};
}]);
我从SO开始采用这个解决方案..虽然现在无法找到原始答案链接......
答案 1 :(得分:0)
angular.module('app', []).controller('homeCtrl', function($scope) {
$scope.limit = 3;
$scope.expand = function(limit) {
$scope.limit += limit;
}
$scope.chat = [{ 'data' : 'one'},{ 'data' : 'two'},{ 'data' : 'three'},{ 'data' : 'four'},{ 'data' : 'five'},{ 'data' : 'six'},{ 'data' : 'seven'},{ 'data' : 'eight'},{ 'data' : 'nine'},{ 'data' : 'ten'},{ 'data' : 'eleven'},{ 'data' : 'twelve'},{ 'data' : 'thirteen'},{ 'data' : 'fourteen'},{ 'data' : 'fifteen'}];
});