如何正确添加“已检查"循环进入单选按钮组? [英] How to correctly add "checked" to a radio button group in a loop?

查看:69
本文介绍了如何正确添加“已检查"循环进入单选按钮组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常checked只需在输入末尾添加

Usually checked just add at the end of input

<input type="radio" name="item" value="value1" checked>
<input type="radio" name="item" value="value2">

但是现在我正在使用* ngFor.我要选择第一个.

But now I am using *ngFor. I want to select the first one.

我尝试执行此操作,但是它不起作用,因为checked不是类.

I try to do this, but it does not work, because checked is not a class.

<div *ngFor="#item of collection; #i = index"">
    <input type="radio" name="item" value="{{item}}" [ngClass]="{'checked':i === 0}">
    <label>{{item}}</label>
</div>

那我该怎么办呢?谢谢!

So how can I do it? Thanks!

推荐答案

<input type="radio" name="item" value="{{item}}" [attr.checked]="i === 0 ? '' : null">

这篇关于如何正确添加“已检查"循环进入单选按钮组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆