Issue with Angular Reactive form: Checkbox checked property not binding correctly when the page initially loads

Looking to achieve Two-way data binding of Checkbox in Angular Reactive forms. After checking the checkbox, I am updating the 'isdateChkd' variable and storing it in the state. Despite the variable being set to TRUE, the checkbox does not get auto-checked upon page load.

Here are the snippets from both the HTML and ts files:

HTML File


<form [formGroup]="form">
   <div class="abc" formControlName="inputDate">
     <div class="xyz">
       <p-calendar formControlName="inputDate" 
       [(ngModel)]="inputDateValue"
       class="abc"/>

       <input type="checkbox" id="checkbox-1" class="abc" name="dateChk"
       name="dateIsChecked" [(ngModel)]="isdateChkd" (change)="chkFunction($event.target.value)" />
    </div>
  </div>

</form>


Component.ts file

public isdateChkd: boolean = false;

 ngOnInit() {

....Other service calls

// Retrieve value for isdateChkd

this.store.select(getValues).takeUntil(this.ngUnsubscribe).subscribe((data) => {

this.isdateChkd = data.defaultUpdate // boolean value retrieved


});

}

chkFunction(value) {
 this.isdateChkd = value; // Update 'isdateChkd' on checkbox check

this.store.dispatch(new updateCheckboxCheckedService(value)); // Dispatch updated value to the store
}

Your assistance is greatly appreciated...

Answer №1

If you want to enable a checkbox with checked attribute, combine it with NgModel.

It's currently functioning properly using checked along with the value isdateChkd. For a live example, feel free to experiment in this stackBlitz demo

To implement similar changes, refer to the code snippet below

    ...
    <input type="checkbox" id="checkbox-1" class="abc" name="dateIsChecked" 
         [(ngModel)]="isdateChkd" (change)="chkFunction($event)" checked="isdateChkd" />
    ...

Keep coding joyfully.. :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the story in javascript

I am trying to customize the legend to display the following values: 80+ (or 80%+) 75-80 70-75 65-70 60-65 55-50 <50% While I have organized the list in descending order, I seem to be facing an issue with getting the less than symbol to function corr ...

Fade in elements from an array using jQuery

Hey there, I'm currently facing an issue with using Javascript to process data within a function. Since I'm new to Javascript, I'm hoping for a simple explanation. My aim is to display each item from an array sequentially, with a fading in ...

Intercepting HTTP requests on specific routes with Angular 4+ using an HTTP Interceptor

I've developed an HTTP_INTERCEPTOR that needs to function on certain routes while excluding others. Initially, it was included in the main app module file. However, after removing it from there and adding it to specific modules, the interceptor conti ...

bidirectional data binding with programmatically generated input boxes

Is it possible to achieve two-way binding with dynamically created checkboxes? quali=[{class:"10",checked:false},{class:"12",checked:true},{class:"others",checked:true}]; <span *ngFor="let q_list of quali; let i=index" class="lists">  <in ...

Issue encountered while trying to import jszip in Angular project

Encountering an error when trying to import jszip into my TypeScript component file. After successfully running npm install jszip and confirming the package is properly installed, I proceed to import it using: import * as JSZip from 'jszip'; Th ...

How to Transfer Deleted List Items from one Unordered List to another in Angular 9 using Event Binding

Greetings to all =) I've recently delved into Angular 9 and I'm really enjoying the component-based approach. To sharpen my skills in property and event binding, I embarked on a project involving building a deck with two lists. English isn't ...

What is the best way to securely store JWT refresh tokens on both the front-end and back-end?

Storing the refresh token on the client side in "Local Storage" poses a significant security risk. If a hacker gains access to this token, they could potentially have everlasting access to the user's account by continually refreshing both access and r ...

"Troubleshooting Typecscript and Angular: Dealing with mismatched argument

How can I resolve this Angular error: (response: HttpResponse<User>) => { which results in the following error message: Argument of type '(response: HttpResponse<User>) => void' is not assignable to parameter of type '(val ...

What is the best way to extract the value from a resolved Promise?

Currently, I am attempting to read a file uploaded by the user and convert it into a String using two functions. The first function is handleFileInput: handleFileInput(event){ setTimeOut(async()=>{ let abcd= await this.convertFileToString(this.fi ...

Hierarchy-based state forwarding within React components

As I embark on the journey of learning Typescript+React in a professional environment, transitioning from working with technologies like CoffeeScript, Backbone, and Marionettejs, a question arises regarding the best approach to managing hierarchical views ...

Use either Jquery or CSS3 to hide a div if one of its inner divs is empty

I have multiple data sets to display in divs with the same class name. Each div contains two inner divs. <div class="div1"> <div class="div2">Abc</div> <div class="div3"><?php echo $row['SOME VALUE']; ?>< ...

Encountering issues with bidirectional data binding functionality

I have integrated a pagination component from ng-bootstrap into a generic component that includes a select dropdown to choose the number of items per page. I triggered an event from this generic component and caught it in the parent component (member-list. ...

A guide on integrating Pug templating engine with ReactJS

For the integration of Pug with a freshly created ReactJS application, I followed these steps: 1. Started by creating a new ReactJS app using create-react-app 2. Next, I referred to the instructions on babel-plugin-transform-react-pug for installing the ...

Ember Component Incorporating Keyboard Input

I recently developed an ember component that features a rectangular block in a green canvas. Everything is working smoothly so far. However, I am facing some challenges with implementing keyboard input commands (A S D W) to navigate the rectangle within t ...

Change the http observable response to an array that can be used with ngFor

My goal is to dynamically load select options based on an API response, using observables in Angular 5 for HTTP requests. However, when trying to parse the response into select options, I encountered the following error: Cannot find a differ supporting o ...

Uploading and saving data to an object in FaunaDB using React hook forms

I am currently facing an issue with uploading/saving data to an object in FaunaDB. Specifically, I am trying to upload a string to a jobProfile object: data: { "jobProfile": { "image": "" "coverImage": " ...

Establishing a TCP connection to a server using Javascript

Currently, I have developed a server daemon that generates various data, such as messages. However, my main focus is on client monitoring. For instance, I have a webpage and I aim to maintain a constant TCP connection to the server in order to display all ...

Using regular expressions to eliminate text located outside of the tags within a string

Presented is a string consisting of an XML string below: var xmlString = "<str>rvrv</str>rvrv<q1>vrvv</q1>vrvrv<q2>rtvrvr</q2>"; I am seeking assistance on how to eliminate text that lies outside the tags (text no ...

Challenges with displaying css/bootstrap classes within angular2

Experiencing difficulties with CSS/Bootstrap integration when displayed in an angular2 component. When attempting to display the CSS and HTML content in the Index.html file (with proper CSS and JS references), everything functions correctly. However, once ...

Using cdk-virtual-scroll-viewport in combination with Angular and flex-layout (row and wrap)

I am interested in implementing virtual scrolling with flexlayout. For example, I would like the following code to be compatible with virtual scrolling: <div fxLayout="row wrap" fxLayoutAlign="start center"> <div *ngFor=&qu ...