Checkbox checked state in Angular 10 - html

I am using Angular 10. I have a checkbox in my html file. In my typescript file I want to see if the checkbox is checked or not and perform some action accordingly. Is there a way to get the checked state of the checkbox like true if it is checked and false otherwise? TIA.

You can use change event to check the state
<input type="checkbox" (change)="onSelect($event.target.checked)">
onSelect(state:boolean) {
console.log("Is Checked? ", state)
}

If you are not using Template Form or Reactive Form,
<input type="checkbox" (change)="changeEvent($event.target.checked)">
For Template Forms,
<input type="checkbox" [(ngModel)]='checked' (ngModelChange)="changeEvent()">
For reactive forms,
<input type="checkbox" formControlName='check'>
this.form.get('check').valueChanges
.subscribe((check)=> this.changeEvent())

To my knowledge, we have two ways of checking the state of the checkbox:
Use the library for angular, Ex: Material UI: https://material.angular.io/components/checkbox/examples or different libraries.
Use two-way binding of angular [(ngModel)] ="checked" on the HTML template

you can use two-way binding like this
html
<input type="checkbox" [(ngModel)]="checked">
ts
checked = false;

Related

Value attribute not working for Radio Button Angular

I am trying to create a dynamic form that includes the Radio Buttons.
<div *ngFor="let opt of question.options">
<input [formControlName]="question.key" [value]="opt.key" [type]="question.controlType" [id]="opt.key" [name]="question.key"/>
<label [htmlFor]="opt.key">{{opt.value}}</label>
</div>
But the value attribute is empty in the HTML when the Page is loaded. What could the possible reason ?
<input _ngcontent-mjy-c129="" ng-reflect-name="gender" type="radio" value="" id="male" name="gender" class="ng-invalid ng-dirty ng-touched">
This problem was solved after removing the FormControl from the input elements. I think the initial value of form control was overriding the new values which I was trying to set.
Be sure you call data before initializing the form

Set value of radio button input using URL Parameter only (WITHOUT JQuery or JavaScript)

I have an HTML form on a web page. I want to send users an email with a URL that they can click to fill out the form. I want to pre-populate the value of a radio button group using URL parameters only.
The platform I am using does not allow me to do any scripting of any kind. I need to do this using only the URL parameters.
This is trivial for other types of input tags. For example, if I have a page called form.html and within that page I have an input tag as follows:
<input name="firstname" type="text">
Then I can use the following URL to pre-populate the field with the value "James":
http://form.html?firstname=James
What I am looking for is how to do this with a radio button. For example, let's say my page form.html has a radio button group with three options as follows:
<input name="status" type="radio" value="New">
<input name="status" type="radio" value="Expired">
<input name="status" type="radio" value="Renewed">
How do I pre-set the value of this radio button group with a URL parameter?
I have tried the following:
http://form.html?status=Expired
But this doesn't work. Is there any way to do this without JS or JQuery? You may think I can just set the value I want to be selected by default using the checked=true attribute in the HTML itself, but the problem is that the value I want to pre-populate is different depending on the user, so I need to set it in the URL parameter.
Came across this when looking for same answer and surprised no one answered it. Bit late but I figured this out and thought I'd put in answer here. Basically, I ran an pageload function and grabbed the parameters via URLSearchParams then selected the radio button via an ID. For example:
Add IDs to your radio buttons:
<input id="new" name="status" type="radio" value="New">
<input id="expired" name="status" type="radio" value="Expired">
<input id="renewed" name="status" type="radio" value="Renewed">
Then run the javascript below upon pageload (add if statements to handle different statuses):
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const status = urlParams.get('status');
if (status == "expired") {
document.getElementById("expired").checked = true;
}
I based this off the following answer but modified it for radio butons: https://stackoverflow.com/a/70267794/7901491

How do I set a default value on a checkbox in Angular?

I have the following code:
<input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]=
{standalone:true} (change)="recovery(i.checkt,i.TherapeuticArea)">
{{i.TherapeuticArea}}
The problem I am facing is that with standalone:true every checkbox is checked by default and when standalone is false, the checkbox isn't working. Is there any way of setting the checkbox value to unchecked while having full functionality for the user?
You need to set the checked attribute on the input like the following:
<input type="checkbox" [(ngModel)]="i.checkt" [ngModelOptions]={standalone:true}
(change)="recovery(i.checkt,i.TherapeuticArea)" [checked]="i.checkt">
But I would recommend #Florian's comment of using a FormControl to manage inputs from the UI. It will save you a lot of time and makes it easier to maintain in my opinion.

can anyone explain why we need ngModel and #nameField="ngModel" both in same input field of angular6 forms?

I am starting with my angular6 and I come across the syntax below.
<input type="text"
class="form-control"
name="company-name"
ngModel
#nameField="ngModel"
required
minlength="3">
now my question is if ngModel and name is already there to uniquely identify form component and ngModel directive to bind it with angular form why we need #nameField="ngModel"?
We can have input value from name="company-name". so why 2 NgModels?
and what is the difference between #nameField="ngModel" and [(ngModel)]="nameField"?
To create a valid template-driven form control - you only have to add name="company-name" and ngModel.
The template reference #nameField="ngModel" can be used as a variable in your html (so it's optional).
[(ngModel)]="nameField" is the two-way data binding in Angular, aka "banana-box" (for more detailed explanation read this article two-way-data-binding-in-angular-2 or the official documentation NgModel )
I always thought it is also needed for validation purposes so the error message show up nicely below the control.

Checkbox checked if boolean is true with Angular2

I would like to know how to make a checkbox checked if the value is true, and unchecked if false with Angular2.
Adult <input type="checkbox" value="{{person.is_adult}}">
{{person.is_adult}} is a boolean
Can someone please suggest anything? Thanks
{{}} does string interpolation and stringifies true and false and Angular by default uses property binding and I assume the property expects boolean values not strings:
<input type="checkbox" [checked]="person.is_adult">
This might work as well
<input type="checkbox" attr.checked="{{person.is_adult}}">
because with attribute binding the browser might translate it from the attribute (which can only be strings) to boolean when reading it into its property.
It is also checked instead of value
You can also use ngModel
<input type="checkbox" [ngModel]"person.is_adult" name="isAdult">
<input type="checkbox" [(ngModel)]"person.is_adult" name="isAdult">
for one-way or two-way binding.
Ensure your have the FormsModule imported if you use ngModel.
you are missing square bracket around checked
<input type="checkbox" [checked]="person.is_adult">
Hope this helps!!
Try the following :
<input type="checkbox" [checked]="person.is_adult">
If you are using ngModel :
When ngModel is used in a form it won't work. However, you should use [ngModelOptions] attribute like
<input
type="checkbox"
name="is_adult"
[(ngModel)]="person.is_adult"
[ngModelOptions]="{standalone: true}"/>