arrays - Angular ngFor 乱序
问题描述
我有几个月的时间。由于某种原因,角度 for 循环不遵循数组的顺序。 这怎么能解决?
@Component({
selector: 'my-app',
template: `<div *ngFor="let item of collection | keyvalue; index as i">{{item | json}}</div>`,
})
export class AppComponent {
collection : {key : any, value : any}[] = [];
constructor(){
let i = 0;
for(let month of moment().locale('en-US').localeData().monthsShort()){
i++;
this.collection.push({key : i,value : month});
}
}
https://stackblitz.com/edit/angular-i25npn?file=src%2Fapp%2Fapp.component.ts
解决方案
根据文档keyvalue
中的定义。
输出数组将按keys 排序。默认情况下,比较器将按 Unicode 点值...
因此,默认情况下您的键(字符串)是有序的。
从| keyvalue; index as i
您的*ngFor
.
推荐阅读
- xml - 如何识别 XML 中的 404 URL?
- javascript - 如何为此 javascript 元素添加背景颜色?
- azure-sql-database - 为什么 azure sql 的 sys.dm_db_missing_index_groups 没有更新?
- flutter - 如何在颤振应用程序中添加对所有语言环境的货币符号的支持
- javascript - Vue 未解析的变量 PhpStorm
- ios - 在 SwiftUI 中,如何在 UIView 内或作为 UIView 使用 UIHostingController?
- php - How to display errors when error occurs using bootstrap in php
- c# - 为什么我的网络摄像头没有在 c# 中流式传输视频?
- nativescript - 将 NativeScript 项目部署到 Web 浏览器?
- laravel - 如何在 Laravel 中正确实现这种多态关系?