我正在嘗試從Angular Docs中建立英雄之旅應用程序。 定義我的英雄陣列並試圖循環播放它後,它只會產生空的div。Angular無法檢索英雄之旅中的英雄列表
這是我下面的代碼:
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",
}
];
爲什麼發生這種情況?
你看到標題 – Sajeetharan
沒有,標題就是空的div – olayemii
檢查,如果你有控制檯 – Sajeetharan