ngFor 中的引用变量不起作用

玛努查达

我想使用 *ngFor 将以下 HTML 代码转换为 Angular 代码

<div>Children: <input type="radio" id="1" name="children" [value]="1" [(ngModel)]="this.children"/>1
      <input type="radio" id="2" name="children" [value]="2" [(ngModel)]="this.children"/>2
      <input type="radio" id="3" name="children" [value]="3" [(ngModel)]="this.children"/>3
      You have {{this.children}} children
      </div>

角码

<div>Children: <input *ngFor = "let option of selectOptions" type="radio" id=option name="children" [value]=option [(ngModel)]="this.children"/>{{option}}
        You have {{this.children}} children
      </div>

selectOptions 在 Component 的类中定义如下:

selectOptions:Array<number>=[1,2,3]

我可以看到错误{{option}}的是Angular: Identifier 'option' is not defined. The component declaration, template variable declarations, and element references do not contain such a member

我也看到错误的id=option name="children"那个tag start is not closed

我究竟做错了什么?

博格丹奇

我这样使用你的代码:

<div>Children: <input *ngFor = "let option of selectOptions" type="radio" id={{option}} name="children" [value]=option />{{option}}
        You have {{this.children}} children
</div>

它适用于这个结果:

在此处输入图片说明

生成这个html:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章