【发布时间】:2019-07-03 16:58:27
【问题描述】:
我正在尝试使用 *ngFor 在 html 中显示我的 json 数组,但我认为它不起作用,因为它需要在 AppComponent 类中。但是,如果我尝试移动源代码以在 AppComponent 类中创建 json 对象,则会出现错误。
这是我的 app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
s = ['Windstorm', 'Bombasto', 'Magneta', 'Tornado'];
myHero = this.s[0];
}
interface Person {
name: string;
year: number;
}
var people: Person[] = [{ name: 'robert', year: 1993 }];
var newPerson: Person = { name: 'joe', year: 1992 };
people.push(newPerson);
newPerson = {name: 'jose', year: 1997};
people.push(newPerson);
console.log(people[2].name);
这是我的 app.component.html 文件
<p>HEllo</p>
<ul>
<li *ngFor="let name of s">
{{ name }}
</li>
</ul>
我想对我在 html 文件中所做的事情做一些事情,但使用另一个数组
【问题讨论】:
-
您能否向我们展示如何您使用 html 上的其他数组来做到这一点?你遇到的
error是什么。
标签: arrays json angular typescript