编辑#2
不幸的是,我没有成功采纳这些建议中的任何一个。很晚了,所以我要去 sleep 了,但我会在明天尝试更新。
感谢 Pradeep Jain 的帮助。我也会尝试邪恶思想建议的管道。
我已经学会了如何使用 *ngFor 命令,但看起来它只适用于简单的数组。这是我尝试遍历的代码。
import {Lesson} from './lesson';
export var LESSONS: Lesson[] = [
{
"idL": 1,
"subject": 'Chapter One',
"points": [
'picture story', 'spelling test', 'words'
]
},
{
"idL": 2,
"subject": 'Words',
"points": [
'words', 'bacon', 'proliferation'
]
}
]
所以我正在尝试访问名为“points”的第二个数组。我不认为它的格式不正确,但我一直无法弄清楚如何访问它。
我确实读过 angularJs 的 foreach 命令,但是 Angular2 的文档没有显示这样的命令可用。
有什么建议吗?
最佳答案
更新 (2.0.0)
<table>
<ng-container *ngFor="let lesson of LESSONS; let i = index">
<ng-container *ngFor="let point of lesson.points; let j = index">
<tr>{{point}}</tr>
</ng-container>
</ng-container>
<ng-container>
是一个没有标记到 DOM 的辅助元素。它允许使用与内联相同的语法 *ngIf
和 *ngFor
原创
试试这个:
<table>
<template ngFor #lesson [ngForOf]="LESSONS" #i="index">
<template ngFor #point [ngForOf]="lesson.points" #j="index">
<tr>{{point}}</tr>
</template>
</template>
工作示例 Working Plunker.
关于arrays - Angular 2 如何遍历多维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37089581/