【发布时间】:2017-04-10 06:49:55
【问题描述】:
我有以下代码,我想使用 *ngFor。该代码仅显示 2 个单选按钮,但还有更多。
options: string[] = [
'Single',
'Married',
'Divorced',
'Common-law',
'Visiting'
];
<input
#single
class = 'with-gap'
name = 'group3'
type = 'radio'
id = 'single'
value = 'Single'
(change) = 'radioBtnChange$.next(single.value)'/>
<label for = 'single'>Single</label>
<input #married
class = 'with-gap'
name = 'group3'
type = 'radio'
id = 'married'
value = 'Maried'
(change) = 'radioBtnChange$.next(married.value)'/>
<label for = 'married'>Married</label>
我如何使用@ngFor 而不是编写单独的单选按钮,以便代码更简洁。
EDIT1 库的语法如下:
<input class="with-gap" name="group3" type="radio" id="test5" checked />
<label for="test5">Red</label>
它是http://materializecss.com/forms.html的一部分
我尝试将输入包装在标签中,但只显示标签。
感谢您的任何意见。
EDIT2
<label *ngFor="let option of options" for='option' >
<input
#option
type="radio"
class = 'with-gap'
name='group3'
id='option'
[value]='option'
(change) = 'radioBtnChange$.next(option.value)'/>
</label>
【问题讨论】: