2016-12-06 25 views
1

试图插补当我用角2 +打字稿,我在选项标签获取楠DOM从阵列ngFor

app.component.ts得到楠:

export class AppComponent { 
    rooms = { 
    type: [ 
     'Study room', 
     'Hall', 
     'Sports hall', 
     'department room', 
     'Service room', 
     'Restroom'] 
    }; 

应用。 component.html:

<select placeholder="Select an option"> 
    <option *ngFor="let room-type of rooms.type">{{room-type}}</option> 
    </select> 

我想不通这是怎么回事,因为我不能调试插值和所有的绑定操作,我只是不知道怎么了,谷歌,必应不帮忙!

我使用Visual Studio代码在Windows 10

这就是它的样子:

enter image description here

我认为这是打字稿导致此问题,因为事情是有望获得数量变量,但收到不是数字...

+3

这似乎是一个坏主意,用符号“ - ”在变量名 –

回答

7

不要在变量名中使用-。它在JS/TS中是不允许的,所以在模板中也是不允许的。它会尝试减去并导致NaN(非数字):

<option *ngFor="let roomType of rooms.type">{{roomType}}</option> 
+0

<选项* ngFor =“让房间roomType。键入“> {{roomType}} – anshuVersatile

+0

{{roomType}},当然。不是{{房型}} –

+0

woopsiekiedeeskes。你是对的:) – PierreDuc