gpt4 book ai didi

javascript - orderBy 过滤器不排序动态添加的 ng-repeat 列表

转载 作者:行者123 更新时间:2023-11-30 10:15:37 25 4
gpt4 key购买 nike

当我向数组动态添加内容时,ng-repeat 不会遵守 orderBy 过滤器。

请看一下我创建的示例: http://jsfiddle.net/fPX7R/2/

<script>
function RedditAuthor (name) {
this.name = name;
}
var redditPostUrl = 'http://www.reddit.com/r/learnprogramming/comments/26lfww/python_why_does_10_2_100_and_not_100.json';
var huebelApp = angular.module('huebelApp', []);
huebelApp.controller('LightCtrl', function ($scope, $http) {
$scope.authors = [];
$http.get(redditPostUrl).success(function(postData) {
for (var i=0; i<postData[1].data.children.length; ++i) {
var commentId = postData[1].data.children[i].data.id;
$http.get(redditPostUrl+'/'+commentId+'.json').success(function(commentData){
var authorName = commentData[1].data.children[0].data.author;
$scope.authors.push(new RedditAuthor(''+authorName));
});
};
});
});
</script>
<body ng-app="huebelApp" ng-controller="LightCtrl">
<div class="row" ng-repeat="author in authors | orderBy:name">
<span>{{author.name}}</span>
</div>
</body>

在这个例子中,我查询了一个 reddit 帖子,并对每个顶级评论发出了一个查询。然后将评论查询的结果添加到 ng-repeat 显示的数组中。然而,订单并没有得到维护。

当动态添加内容时,我如何告诉 Angular 遵守过滤器并对列表进行排序?

最佳答案

orderBy 缺少引号...

<body ng-app="huebelApp" ng-controller="LightCtrl">
<div class="row" ng-repeat="author in authors | orderBy:'name'">
<span>{{author.name}}</span>
</div>
</body>

Updated Fiddle

关于javascript - orderBy 过滤器不排序动态添加的 ng-repeat 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23896357/

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