我正在尝试从 Angular Docs 构建英雄之旅应用程序。在定义我的英雄阵列并尝试循环遍历它之后,它只会显示空 div。
这是我下面的代码:
html
<h1>{{title}}</h1>
<ul class="heroes">
<li *ngFor="let hero of heroes" (click)="onSelectHero(hero)">
<span class="badge">{{hero.id}}</span>{{hero.name}}
</li>
</ul>
<div>
<h2>{{selectedHero.name}} details!</h2>
<div><label>id: </label>{{selectedHero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="selectedHero.name" placeholder="name"/>
</div>
</div>
打字稿
import { Component } from '@angular/core';
import { Hero } from './shared/model/hero';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title:string;
heroes: Hero[];
selectedHero: Hero;
constructor(){
this.title = 'app';
this.heroes = HEROES;
}
onSelectHero(hero: Hero){
this.selectedHero = hero;
}
}
const HEROES: Hero[] = [
{
id: 11,
name: "NightOwl",
},
{
id: 12,
name: "Batman",
},
{
id: 13,
name: "Superman",
},
{
id: 14,
name: "Spiderman",
},
{
id: 15,
name: "Hulk",
}
];
为什么会这样?