AngularBeg
AngularBeg

Reputation: 33

How to check on button click is the radio button checked angular2/typescript

In my html :

<input #myRadio type="radio" name="category" id="work-time" />


<button (click)="myClick(myRadio)">save</button>

In my component:

myClick(_myRadio:HTMLElement)
{

}

I have tried to save the result in a boolean type variable, but there is no attribute isChecked or Value, so I am stuck in this.

How to check if the radio button is checked on the button click by the user?

Upvotes: 0

Views: 4831

Answers (1)

stealththeninja
stealththeninja

Reputation: 3791

Try ngModel:

<input type="checkbox" name="category" [(ngModel)]="category" />
<button (click)="myClick()">save</button>

NgModel binds the checkbox value (if checked true otherwise false) to a class property category. The method called by button click myClick() can then use the property:

myClick() {
  // do stuff with this.category;
}

If you'd prefer to not import the forms module, you can use property and event bindings on the input to accomplish the same result:

<input type="checkbox" name="category" [checked]="category" (click)="category = !category" />
<button (click)="myClick()">save</button>

Upvotes: 1

Related Questions