繁体   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