Angular 2 - 使用模型驱动选择(html)选择值

时间:2016-09-14 10:49:39

标签: html angular

我有以下选择(PUG)

select.custom-select(name='category', formControlName='category')
    option(*ngFor="let category of essentials.categories") {{category.name}}

此代码有效,但我想将表单控件绑定到category.code,例如:

select.custom-select(name='category', formControlName='category')
    option(*ngFor="let category of essentials.categories" [ngValue]="category.code") {{category.name}}

使用上面的代码,我收到了错误(Can't bind to 'ngFor' since it isn't a known property of 'option')。我也试过[value]="category.code"没有成功......

编辑:我使用的是Angular 2 RC7

1 个答案:

答案 0 :(得分:1)

尝试使用引号将其隐藏*ngFor(请记住[ngValue]

select.custom-select(name='category', formControlName='category')
       option("*ngFor"="let category of essentials.categories", "[ngValue]"="category.code") {{category.name}}