gpt4 book ai didi

javascript - AngularJS NG重复: limitTo not limiting object loop

转载 作者:行者123 更新时间:2023-12-02 14:59:25 24 4
gpt4 key购买 nike

我想有限制地收集过去的聊天记录,但这会打印出所有聊天记录。

 <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>

这是 Controller 中的代码。

    $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"

最佳答案

限制不适用于对象,因为您需要实现自己的自定义过滤器:

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中获取了这个解决方案..虽然现在无法找到原始答案链接...

关于javascript - AngularJS NG重复: limitTo not limiting object loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com