1

I designed custom radio button, this is working proper in static but when i make dynamic, that time it act like checkbox.

<div *ngFor="let currentwork of currentworks">
    <label class="customcheck">{{currentwork}}
        <input type="radio" name="{{currentwork}}">
        <span class="checkmark"></span>
    </label>&nbsp;
</div>            

If I put name={{currentwork}}, it will act like checkbox. I want radio button with one selection. I don't know why it's coming like this, please help me.

2
  • 1
    Radio buttons need to have the same name attribute in order to work all like one object. Maybe you wanted to set the value Commented Jan 8, 2019 at 11:10
  • @Cristian Traìna i tried with value also it's not working.. can you please tell briefly.. Commented Jan 8, 2019 at 11:13

4 Answers 4

2

Names of radio buttons must be the same.

This may works:

<div *ngFor="let currentwork of currentworks">
  <label class="customcheck">{{currentwork}}
    <input type="radio" name="currentworks" [value]="currentwork">
    <span class="checkmark"></span>
  </label>&nbsp;
</div>   
Sign up to request clarification or add additional context in comments.

5 Comments

Don't forget the value
@Cristian Traìna value means?? sry, i can't get it.
The value is what's passed on submit, it needs to be different for any input radio
@CristianTraìna Thanks for your advice :) I edited my post
@CristianTraìna thank you, i have one doubt if i use formControlName , i get error like name and formcontrol must be match. Is any possible implementing of radio button without name property?
2

You need to set the value, not the name, for each option. The name should be the same for each option. You can also use the binding bracket notation [value] over value="{{}}.

<div *ngFor="let currentwork of currentworks">
  <label class="customcheck">{{currentwork}}
    <input name="myRadioButton" type="radio" [value]="currentwork">
    <span class="checkmark"></span>
  </label>&nbsp;
</div>  

Stackblitz

Comments

1

With reactive form approach, you can use like -

<div *ngFor="let currentwork of currentworks">
  <label class="customcheck">
     <input type="radio" [value]="currentwork.id" 
       formControlName="radiobuttoncontrolname">
     <span class="checkmark"></span>
     <span class="customcheck">{{currentwork.Name}}</span>
  </label>
</div>

I am assuming that you are having your currentWorks as -

currentWorks = [
 { id: 'something', Name: 'something' },
 { id: 'something2', Name: 'something2' }
];

Comments

1

For radio buttons to work you need to give the same name and for each option give a value.

A working example should be:

<div *ngFor="let currentwork of currentworks">
    <label class="customcheck">{{currentwork}}
    <input type="radio" name="options" value="{{currentwork}}">
    <span class="checkmark"></span>
    </label>&nbsp;
</div>  

As you can see I give a name (options) that is the same for all input values, and then using currentwork as value.

Here a working example (check app.ts)

Edit: you can even generate multiple radio selection. But you should share an example of your dynamic data.

http://next.plnkr.co/edit/zuhY7kgzPj6141oY?preview

2 Comments

i have dynamic data, if i add that radio button field array, that time i can't give same name for each options , In that case what i do ? please help me. I am struggling in that case also..
@induECE You should share a full example of your dynamic data. I have update the Plunker so you can see as an example where the template has multiple radio input selection, so two radio selection with different name. All depends of how is done your dynamic data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.