gpt4 book ai didi

javascript - 简单的 orderBy 过滤器在 AngularJs 中不起作用

转载 作者:行者123 更新时间:2023-11-28 18:37:19 27 4
gpt4 key购买 nike

我有一个简单的程序,可以按“年龄”对列表进行排序。但它不起作用并给出错误!请帮忙。

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
See here: <p ng-repeat="x in persons | orderby: age">{{x.name}},{{x.age}}</p>
<script>
//module declaration
var app = angular.module("myApp",[]);
//controller declaration
app.controller('myCtrl',function($scope){
$scope.persons=[
{name:"Peter",age:23},{name:"Lina",age:36},{name:"Robert",age:31}
];
});
</script>
</body>
</html>

最佳答案

您的代码有错误。您需要使用 orderBy:'-age'"

将您的代码更改为此并且它可以工作

See here: <p ng-repeat="x in persons | orderBy:'-age'">{{x.name}},{{x.age}}</p>

下面的工作演示

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
See here: <p ng-repeat="x in persons | orderBy:'-age'">{{x.name}},{{x.age}}</p>
<script>
//module declaration
var app = angular.module("myApp",[]);
//controller declaration
app.controller('myCtrl',function($scope){
$scope.persons=[
{name:"Peter",age:23},{name:"Lina",age:36},{name:"Robert",age:31}
];
});
</script>
</body>
</html>

关于javascript - 简单的 orderBy 过滤器在 AngularJs 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858308/

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