角度2 - 绑定不起作用

时间:2017-03-29 14:06:30

标签: angular data-binding

我正在尝试在模板上显示一些简单的数据,但这不起作用。最终输出显示为:

{{message}}
ID  Name
{{item.id}} {{item.name}}

这是代码。我究竟做错了什么?

list.component.html

{{message}}
<table >
    <tr>
        <td>ID</td>
        <td>Name</td>
    </tr>
    <tr *ngFor="let item of items" >
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
    </tr>

</table>

list.component.ts

import {Component, OnInit, ViewChild, Renderer, EventEmitter, ElementRef} from '@angular/core';

@Component({
    templateUrl: 'app/list/list.component.html'
})

export class ListComponent implements OnInit {

    public message:string;

    items: [{id: number, name: string}];

    ngOnInit() {
        this.message = "hello";
        this.loadList();
    }

    loadList() {
        console.log("hello2");

        this.items = [{ id: 1, name: "Test1" }];
    }

}

0 个答案:

没有答案