1

I can't seem to get the data in this.heroes to display on the list. I put an alert(this.heroes[0].name); and it did display the data so this._service.getHeroes(); is returning the data

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();
    }
}
2
  • Where did you put the alert()? Can you make a plnkr or jsfiddle? Commented Mar 3, 2016 at 10:27
  • Did this work with the old ng-repeat="hero in heroes". Read this for reference: coryrylan.com/blog/angular-2-ng-for-syntax Commented Mar 3, 2016 at 11:05

2 Answers 2

1

Try this first and see if it works:

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

If this works you might have an older angular version that does not support the *ngFor syntax yet (I think this is supported in the new version angular 2).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Lisa I'll try it. Its also occurred to me that I might need a number property for *ngFor to work. My Heores class only has a string property for name.
My package.json and index.html weren't properly configured for Angular 2 and dependencies. It works fine now. Thanks Lisa
Ah! I see. I'll keep this in mind when I switch to angular 2.
0

I've done a little more research and no data is displaying. For example:

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

gets a blank line (where myHero is a populated string) whereas

<h2>My favorite hero is </h2>

displays

My favourite hero is

I'm going to change the Beta version of Angular 2 to see if it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.