<tr ng-repeat="data in PackagesData | orderBy : filterOption">
<td>{{data.name}}</td>
<td>{{data.packageType}}</td>
<td>{{convertDateFormatNormal(data.createdDate)}}</td>
</tr>
$scope.convertDateFormatNormal = function (date) {
if(date){
var dateFormatChange = new Date(date);
var dateChanged = $filter('date')(dateFormatChange, "dd/MM/yyyy");
return dateChanged;
}
};
orderBy 中的filterOption 可以是任何名称、packageType 或createdDate。但名称和包类型按预期工作,但 orderBy 不适用于createdDate,因为我将日期格式从 mm/dd/yyyy 转换为 dd/mm/yyyy 以在 HTML 中显示,但它仍然仅按 mm/dd/yyyy 顺序排序。
最佳答案
在 Angular JS 中,排序取决于范围变量的格式,而不是 html 中的打印格式。如果您想使用 dd/mm/yyyy 格式对其进行排序,则必须在 js 文件中格式化日期,并通过循环遍历“PackagesData”对象将其存储在“modified_date”等键中,然后输入 {{modified_date}}在 html 中并按“modified_date”过滤它。 我曾经遇到过同样的问题,但我是这样解决的。
关于angularjs - 当我格式化 html 中的内容时,orderBy 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609662/