我似乎無法獲得這個數據。這個英雄顯示在列表中。我把一個警報(this.heroes [0] .name);它顯示的數據如此this._service.getHeroes();是返回數據Angular 2 * ngFor不顯示數據
app.html
<div style="width:1000px;margin:auto">
<ul>
<li *ngFor="#hero of heroes">
<span>{{hero.name}}</span>
</li>
</ul>
</div>
app.component.ts
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {HeroService} from './heroes/hero.service';
import {Hero} from './heroes/hero';
@Component({
selector: 'my-app',
templateUrl: 'templates/app.html',
providers: [HeroService],
directives: [ROUTER_DIRECTIVES]
})
export class AppComponent {
heroes: Hero[];
constructor(private _service: HeroService) { }
ngOnInit() {
this.heroes = this._service.getHeroes();
}
}
你把alert()放在哪裏?你可以做一個plnkr或jsfiddle嗎? – martin
這是否與舊的ng-repeat =「英雄英雄」一起工作。閱讀本文以供參考:https://coryrylan.com/blog/angular-2-ng-for-syntax –