checkbox selected will not trigger the event

Whenever the checkbox is checked, the events are not triggering. Check out this stackblitz demo code for reference. I am looking to make the checkbox trigger events without the need to physically click on it. Therefore, I am using a button to simulate the event.

HTML

<div class="card m-3">
    <div class="card-body">
        <form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
            <div class="form-row">
                
                <div class="form-group col-5">
                    <label>First Name</label>
                    <input type="text" formControlName="firstName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.firstName.errors }" />
                    <div *ngIf="submitted && f.firstName.errors" class="invalid-feedback">
                        <div *ngIf="f.firstName.errors.required">First Name is required</div>
                    </div>
                </div>
                <div class="form-group col-5">
                    <label>Last Name</label>
                    <input type="text" formControlName="lastName" class="form-control" [ngClass]="{ 'is-invalid': submitted && f.lastName.errors }" />
                    <div *ngIf="submitted && f.lastName.errors" class="invalid-feedback">
                        <div *ngIf="f.lastName.errors.required">Last Name is required</div>
                    </div>
                </div>
            </div>
            
            
            <div class="form-group form-check">
                <input type="checkbox" class="form-check-input" [(ngModel)]="test" (focus)="eventCheck(registerForm.valid,$event)"
                [ngModelOptions]="{standalone: true}"/>
                <label for="acceptTerms" class="form-check-label">Accept</label>
                <div *ngIf="submitted && f.acceptTerms.errors" class="invalid-feedback">Accept Ts & Cs is required</div>
            </div>
        </form>
    </div>
</div>

<div class="text-center">
<button class="btn btn-primary mr-1" (click)="focusCheckbox()">Tick Checkbox</button>
</div>

ts

focusCheckbox() {
   this.test = true;
 }
eventCheck(v: boolean ,event){
  console.log(event.target.checked);
  console.log(v);
}

Answer №1

Utilize the blur or focusout event.

Swap out

<input type="checkbox" class="form-check-input" [(ngModel)]="test" (focus)="eventCheck(registerForm.valid,$event)"

With :

  <input type="checkbox" class="form-check-input" [(ngModel)]="test" (blur)="eventCheck(registerForm.valid,$event)"

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

Indexing types unexpectedly behaving and generating errors that were not anticipated

I find the code below quite strange... export class Collection { private data: {[k: string]: any} = {}; constructor () { // This part works fine this.data["hello"] = "hello"; // Unexpectedly works this.data[2] = 2; ...

Angular 10 recommends utilizing the "forEach" method in place of "map" since the return value of "map" is not being utilized in this specific context

I encountered the following error message: Consider utilizing "forEach" instead of "map" since the return value is not being utilized in this case. declare const require: { context(path: string, deep?: boolean, filter?: RegExp): { keys(): string[]; (id: ...

Typescript controller inheritance leading to Error: $injector:unpr Unknown Provider due to minification

LATEST UPDATE: 2019/07/16 The issue I am facing is actually a result of misusing $inject. Instead of declaring it as private $inject in api-service.ts, it should have been public static $inject = [...]. During the minification process, explicit injection ...

Using AngularFire2 to manage your data services?

After diving into the resources provided by Angular.io and the Git Docs for AngularFire2, I decided to experiment with a more efficient approach. It seems that creating a service is recommended when working with the same data across different components in ...

Unleashing the full power of TypeScript: Harnessing Array.prototype.reduce with Generics

Attempting to standardize data using Array.prototype.reduce. Puzzled by how I can bypass "type-checking" in the reduce function. My interfaces: interface Dict<T> { [key:string]: T; } interface InnerData { id: string; value: number; } inter ...

When trying to click a button in an ion-footer, the (click) function may not execute initially

I have been working on creating a chat feature with Ionic 2, utilizing the keyboard-attach directive. //View <ion-footer [keyboardAttach]="content"> <ion-toolbar class="text_send"> <ion-textarea class="textarea" fz-elastic ...

Angular 9: Extracting URL from a string

Just diving into Angular9 and looking to extract a specific substring? src=/sites/default/files/text_images/cover/gita.jpg (found inside an img tag in the string). Afterward, I aim to create a new URL: If you have insights on how to achieve this, pleas ...

Enhanced hierarchical organization of trees

I came across this code snippet: class Category { constructor( readonly _title: string, ) { } get title() { return this._title } } const categories = { get pets() { const pets = new Category('Pets') return { ge ...

Guide to dynamically render a React component depending on the value of a prop

Utilizing the icon component from the '@fluentui/react-northstar' library looks like this: import { QnaIcon } from '@fluentui/react-northstar' const Example1 = () => ( <> <QnaIcon size="large" /> </&g ...

Can one create a set of rest arguments in TypeScript?

Looking for some guidance on working with rest parameters in a constructor. Specifically, I have a scenario where the rest parameter should consist of keys from an object, and I want to ensure that when calling the constructor, only unique keys are passed. ...

How can I dynamically resize an element based on the height of its content in Ionic?

Is there a way to make my element expand conditionally? I would like it to expand on click based on the height of its content. This is what the component's HTML looks like: <div class="container" [style.height]="enlarged === true ? ...

There seems to be an issue with the type error regarding the return of the mysql2/promise

As I delve into using the mysql2/promise library with Typescript, I've encountered a puzzling issue regarding the return type of the query method. Despite my best efforts, I can't seem to resolve an error in my code. Here is a snippet from my c ...

What is causing TypeScript to incorrectly infer rest parameters in function implementation?

I have created a function that takes a string name and a varying number of parameters which should be inferred from a specific interface. While calling this function results in correct inference, the type is not narrowed down within the function itself. in ...

Is there a way to display unused imports and locals as warnings instead of errors in vscode?

When I'm writing TypeScript code in my code editor, it highlights unused imports and local variables as errors, marked with a red squiggly underline: https://i.sstatic.net/zRJMP.png However, I would prefer if these were shown as warnings in the edit ...

Adding an external JavaScript library to a gateway project: A step-by-step guide

I've been attempting to integrate the Simpl5 JavaScript library into my gateway, but I have encountered some issues. I placed SIPml-api.js and SIPml.js in the webapp/content/scripts directory. In .angular-cli.json, I updated the scripts array as follo ...

There seems to be an issue with the Angular9 window.open() function as it is

I've encountered a strange issue where the code runs successfully on my local environment, but in production, I keep receiving a bad request 400 error. Any insights on what might be causing this? Here is the URL that triggers the bad request error: h ...

How to integrate a toggle switch into an Angular datepicker component

Can the toggle switch be placed inside the calendar? I would like to have it positioned at the top of the open calendar so that users can choose to view date details using the toggle switch <form #uploadForm="ngForm" (keydown.enter)="$event.preventDe ...

Choose all the checkboxes that use Knockout JS

Struggling with implementing a "select all" checkbox feature as a Junior developer on a complex project utilizing knockout.Js and Typescript. I can't seem to figure out how to select all existing checkboxes. Here is the HTML: <td> <inp ...

Discovering Spring Boot Properties Within Angular Application

Situation: One microservice is built using Spring Boot and Angular 8. In the Spring Boot application, there is a properties file called application.yaml with the following content: url-header: http gateway: host: envHost:envPort ui-endpoints: g ...

Scrolling horizontally in Ionic framework

In regards to the response found on Ionic - Horizontal scroll tab for Categories, I have a question. I am curious about what needs to be included in the category.model. Can anyone provide some guidance? ...