【问题标题】:Angular 2 - Set value of (html) select with model-drivenAngular 2 - 使用模型驱动设置(html)选择的值
【发布时间】:2016-09-14 20:58:36
【问题描述】:

我有以下选择(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

【问题讨论】:

  • 你试过这样select.custom-select(name='category', formControlName='category', '*ngFor'="let category of essentials.categories", '[ngValue]'="category.code") {{category.name}}吗? `
  • @mic4ael 不,但它似乎不正确......循环必须在选项而不是选择中(根据所有文档)..我会试一试跨度>
  • 它不起作用:(

标签: html angular


【解决方案1】:

尝试通过用引号将*ngFor 转义(还要记住[ngValue]

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

【讨论】:

  • 就是这样,谢谢!你能解释一下为什么必须用引号括起来吗?
  • 这是必要的,因为您使用了一些特殊字符。 github.com/pugjs/pug/issues/2270
猜你喜欢
  • 2017-04-20
  • 2017-04-29
  • 1970-01-01
  • 2017-02-05
  • 2018-10-22
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
相关资源
最近更新 更多