2016-11-25 7 views
1

* ngForを使用したい次のコードがあります。コードには2つのラジオボタンしか表示されていませんが、もっと多くのラジオボタンがあります。ラジオボタン生成のための@ngForテンプレートの使用

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 libararyの構文は次のとおりです。

<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> 

答えて

1

問題は、テンプレート変数名だけでなく、*ngFor現在の反復値としてoptionを使用していました。だから*ngFor#optionテンプレートのテンプレートradioで上書きされてしまったのです。

あなたはuが動作するはずです、私は信じている、少なくともAngular2のRC2を、使用している場合*ngFor="let option of options"

*ngFor="let opt of options"にマークアップ

<label *ngFor="let opt of options" for='option' > 
    <input 
     #option 
     type="radio" 
     class = 'with-gap' 
     name='group3' 
     id='option' 
     [value]='option' 
     (change) = 'changed(opt)'/> 
</label> 

Demo Here

+0

これまでと同じように、何も表示されませんでした。 –

+0

何が起こったのでしょうか? –

+0

私は最初にそれを包んだとき自分自身と思った - 私はタイプ= 'ラジオ'を持っていた。しかし、それはしませんでした。エラーはありません。何も表示されませんでした。私はdivとラベルでeverthingを置く。ラベルは表示されますが、* ngForテンプレートからは何も表示されません。 を使用しているライブラリの構文を覚えておいてください。 l'at [http://materializecss.com/forms.html](http://materializecss.com/forms.html)#radio –

0

を仮定し、変数名のいずれかを変更する必要があります。

<label *ngFor="let option of options"> 
    <input 
    #option 
    type="radio" 
    class = "with-gap" 
    name="group3" 
    id="option" 
    [value]="option" 
    (change) = "radioBtnChange$.next(option.value)"/> 
</label> 

「代わりに」と「=オプション」のものには注意してください!

+0

残念ですが、何も表示されません。私は角度〜2.1.2を使用しています –

関連する問題