最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 create a list from array - Stack Overflow

programmeradmin4浏览0评论

Using Angular2 I'm trying to make a list from an array...something like:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: `

        <ul>
            <li *ngFor="">
                {{names}}
            </li>
        </ul>

    `,
})
export class AppComponent {

    names = ['name1', 'name2', 'name3'];

}

How can I get something like this to work?

Using Angular2 I'm trying to make a list from an array...something like:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: `

        <ul>
            <li *ngFor="">
                {{names}}
            </li>
        </ul>

    `,
})
export class AppComponent {

    names = ['name1', 'name2', 'name3'];

}

How can I get something like this to work?

Share Improve this question asked Apr 11, 2016 at 22:23 Satch3000Satch3000 49.5k90 gold badges225 silver badges349 bronze badges 1
  • Did you really have this problem? I would think any documentation with NgFor would demonstrate how to do this. I.e., I think it would take you longer to post this question then it would to find the answer with a simple Internet search. Questions are supposed to show some research effort. Obviously you know that something like #ngFor="" isn't going to work. You should show what you tried, and where you got stuck, or why you couldn't figure this out based on some documentation. – Mark Rajcok Commented Apr 12, 2016 at 14:51
Add a ment  | 

2 Answers 2

Reset to default 7

Should be

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: `
        <ul>
            <li *ngFor="#name of names">
                {{name}}
            </li>
        </ul>
    `,
})

export class AppComponent {

    names = ['name1', 'name2', 'name3'];

}

If you came here looking for Angular Framework v2's syntax - not AngularJS v2 like me here it is:

        <ul>
            <li *ngFor="let name of names;">
                {{name}}
            </li>
        </ul>

Not saying anyone did anything wrong in the question or anything, their versioning and angular v.s. older angularjs is confusing IMO

发布评论

评论列表(0)

  1. 暂无评论