在 angular ng-repeat 列表末尾添加项目
Add items end of angular ng-repeat list
我有一个以 10 项开头的 ng-repeat 列表。
我想在列表末尾添加新项目(底部)
如果我目前 运行 该功能,它会将项目添加到我的列表顶部。
我知道您可以使用 .push() 和 .unshift() 添加项目,但这不适用于我使用的代码。
这是我的代码(我使用 Pubnub 中的代码片段来获取历史记录项):
$scope.messages = [];
$scope.loadMore = function() {
var timeToken = $('.lastMessage').attr('timetoken');
console.log(timeToken);
// Populate message history
PubNub.ngHistory({
start: timeToken,
channel: $scope.channel,
count: 5,
});
};
html:
<ion-item ng-repeat="message in messages track by $index">
您可以将 orderby 与 ng-repeat 一起使用。看起来您在每个元素中都有一个时间值,您可以使用它来对列表进行排序。
<tr ng-repeat="item in history | orderBy:'start'">
如果你在元素中有一个 id 你也可以使用 "track by" 所以如果你有一个 id 字段。这允许 angular 显示重复项。但是,这似乎不是您的问题。
来自他们的维基:
By default, ngRepeat does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements.
如果您确实需要重复重复的项目,您可以使用 track by expression 将默认跟踪行为替换为您自己的行为。
<tr ng-repeat="item in history track by $id">
我有一个以 10 项开头的 ng-repeat 列表。 我想在列表末尾添加新项目(底部) 如果我目前 运行 该功能,它会将项目添加到我的列表顶部。
我知道您可以使用 .push() 和 .unshift() 添加项目,但这不适用于我使用的代码。
这是我的代码(我使用 Pubnub 中的代码片段来获取历史记录项):
$scope.messages = [];
$scope.loadMore = function() {
var timeToken = $('.lastMessage').attr('timetoken');
console.log(timeToken);
// Populate message history
PubNub.ngHistory({
start: timeToken,
channel: $scope.channel,
count: 5,
});
};
html:
<ion-item ng-repeat="message in messages track by $index">
您可以将 orderby 与 ng-repeat 一起使用。看起来您在每个元素中都有一个时间值,您可以使用它来对列表进行排序。
<tr ng-repeat="item in history | orderBy:'start'">
如果你在元素中有一个 id 你也可以使用 "track by" 所以如果你有一个 id 字段。这允许 angular 显示重复项。但是,这似乎不是您的问题。 来自他们的维基:
By default, ngRepeat does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements.
如果您确实需要重复重复的项目,您可以使用 track by expression 将默认跟踪行为替换为您自己的行为。
<tr ng-repeat="item in history track by $id">