Angular 我需要[(ngModel)]的所有单选按钮
我只选择了一个按钮,而我只需要一个。当我点击任何按钮时,只有最后一个按钮被选中Angular 我需要[(ngModel)]的所有单选按钮,angular,typescript,Angular,Typescript,我只选择了一个按钮,而我只需要一个。当我点击任何按钮时,只有最后一个按钮被选中 component.html <input type="button" (click)="someFunction(categoryInput.value)" value="click me too" /> <div id="categorybox"> <tr class="categories" *ngFor="let category of categori
component.html
<input type="button" (click)="someFunction(categoryInput.value)" value="click me too" />
<div id="categorybox">
<tr class="categories" *ngFor="let category of categories">
<td>
<input type="radio" id={{category.categoryName}} [(ngModel)]=selectedCategory name="category" value="{{category}}" (click)=selectCategory(category)/>
<label for ={{category.category}} >{{category.categoryName}}</label>
</td>
</tr>
你的代码中有很多错误。指出几点 在
div
标记内不能有tr
您的许多属性中缺少引号
输入
标记不需要结束标记
等等
假设您将类别设置为
categories: any[] = [
{
id: 1,
name: "Category 1"
},
{
id: 2,
name: "Category 2"
},
{
id: 3,
name: "Category 3"
},
{
id: 4,
name: "Category 4"
}
];
您的模板应该如下所示
<table>
<tr class="categories" *ngFor="let category of categories">
<td>
<input type="radio" id="{{category.id}}" name="category" value="{{category.id}}" (click)="SelectCategory(category)">
<label for ={{category.id}}>{{category.name}}</label>
</td>
</tr>
</table>
Stackblitz at您是否可以创建一个Stackblitz来复制您的HTML无效的注释;
不能是
的子级。
<table>
<tr class="categories" *ngFor="let category of categories">
<td>
<input type="radio" id="{{category.id}}" name="category" value="{{category.id}}" (click)="SelectCategory(category)">
<label for ={{category.id}}>{{category.name}}</label>
</td>
</tr>
</table>
this.SelectCategory = category;