簡體   English   中英

角度2 * ng用於不顯示數據

[英]Angular 2 *ngFor not displaying data

我似乎無法在this.heroes中獲取數據以顯示在列表中。 我發出了警報(this.heroes [0] .name); 並且確實顯示了數據,因此this._service.getHeroes(); 正在返回數據

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();
    }
}

首先嘗試一下,看看是否可行:

<li ng-repeat="hero in heroes">
    {{hero.name}}
</li>

如果此方法可行,則您可能有一個較舊的角度版本,尚不支持* ngFor語法(我認為新版本的angular 2中支持此功能)。

我做了更多的研究,沒有數據在顯示。 例如:

<h2>My favorite hero is {{ myHero }}</h2>

獲取空行(其中myHero是填充的字符串),而

<h2>My favorite hero is </h2>

顯示器

我最喜歡的英雄是

我將更改Angular 2的Beta版,以查看是否有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM