angular - *ngFor 不显示任何内容
问题描述
我的代码有问题。我尝试使用 *ngFor 来显示一些数据,但我不知道我的代码有什么问题。这是它:在 src/app/mock-heroes.ts 文件中:
import { Hero } from './hero';
export const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
在我的 src/heroes/heroes.component.ts 文件中,我想像这样显示它:
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
hero = HEROES;
selectedHero: Hero;
constructor() { }
ngOnInit() {
}
}
在 src/heroes/heroes.component.html 文件中,
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes">
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
我没有发现这些代码有什么问题。
解决方案
hero.component.ts
export class HeroesComponent implements OnInit {
heroes : Hero = HEROES;
hero.component.html
<li *ngFor="let hero of heroes">
推荐阅读
- python - 我发生了错误异常:TypeError 元组索引必须是整数或切片,而不是 str(Open CV)?
- javascript - 无法推送嵌套数组中子项的数据子项
- vba - 将表存储在内存中(ArrayList?)
- javascript - Reducer 没有将数据保存到 React Context API 中的状态
- node.js - Gulp 在正常工作一年多后停止工作,现在在命令行中出现“无法识别术语 'gulp'”错误
- python - 从 Python 中的列表中获取对象?
- javascript - 动态表单创建和添加输入字段
- calendar - google calendar api service.calendars().clear(calendarId=calenderID).execute() 返回无效值
- r - Mac上的R RSelenium rsDriver chrome浏览器错误
- java - 将 SeekBar 添加到 SettingsActivity