2016-10-11 113 views
0

的实际问题是,我的角度完全不HTML变量忽略双括号

工作,如果你要测试的这款做到这一点:

import { Component, OnInit} from '@angular/core'; 

@Component({ 
    selector: 'my-app', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent implements OnInit { 

    ngOnInit() { 
     console.log('test'); 
    } 
} 

////////// ///////////////////////////////

/////////////// //////////////////////////

//////////////////// /////////////////////

/////////////////////

我是试图创建一个名称和值彼此不同的下拉列表。在Visual Studio 2015在括号 “> {{directionOption.name}} <” 被示出灰色作为纯文本

<select [(ngModel)]="direction"> 
<option *ngFor="#directionOption of directionOptions" [value]="directionOption.value">{{directionOption.name}}</option> 
</select> 

相同的结果

<select [(ngModel)]="direction"> 
<option *ngFor="let directionOption of directionOptions" [value]="directionOption.value">{{directionOption.name}}</option> 
</select> 



export class AppComponent implements OnInit { 

direction: any = { 'Left': 'L' }; 
directionOptions: any = [{ name: 'Left', value: 'L' }, { name: 'Right', value: 'R' }, { name: 'None', value: 'N' }]; 
} 

浏览器F12内的当前结果

<option *ngfor="#directionOption of directionOptions" [value]="directionOption.value">{{directionOption.name}}</option> 

These variable names are in the same setup as above, just dutch

+0

'directionOptions'没有一个'name' –

+0

是。 @GünterZöchbauer是正确的。 – micronyks

+0

我缺少哪个名字@GünterZöchbauer – Kapein

回答

1

使用let关键字而不是#

<option *ngfor="let directionOption of directionOptions" [value]="directionOption.value">{{directionOption.name}}</option> 
+0

试过了,没有区别似乎是 – Kapein

+0

确切的问题是什么? – micronyks

+0

很可能是我的数组不能用于* ngFor,可能我需要调整 – Kapein

1

更新

Angular2不处理动态添加到DOM绑定,只有当它们被静态地添加到组件模板。

directionOptions应该像

directionOptions: any = [{ name: 'Left' , value: 'L' }, { name: 'Right' , value: 'R' }, { name: 'None' , value: 'N' }];