Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check radio button in angular 2 when there is ngModel

In my Angular 2 project there is ngModel and the radio button doesn't check it

<input type="radio" id="user" name="user" value="RETAIL" [(ngModel)]="myRadio" check>

But when I cut the ngModel it check

<input type="radio" id="user" name="user" value="RETAIL check>

But I can't cut it because I need ngModel, is there another way to check radio button on the load page with ngModel?

like image 543
Dell Avatar asked Oct 21 '25 08:10

Dell


1 Answers

If the value of the input element is a string literal, declare it using one of the two following forms:

<input type="radio" id="user" name="user" value="My value 1" [(ngModel)]="myRadio" />
<input type="radio" id="user" name="user" [value]="'My value 1'" [(ngModel)]="myRadio" />

If the value is not a string literal, use [value]:

<input type="radio" id="user" name="user" [value]="MyValue1" [(ngModel)]="myRadio" />

To have the radio button selected by default, you should initialize myRadio to the appropriate value:

For a string value:

public myRadio: string = "My value 1";

For another type of data:

public myRadio: MyDataType = MyValue1;

You see the code at work in this plunker.

like image 51
ConnorsFan Avatar answered Oct 23 '25 01:10

ConnorsFan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!