Angular function triggered with a double click

In my application, I have created a custom button component that triggers a specific function from the parent component where it is imported. For example, in a login form, clicking the button should call the login() function.

The issue I'm facing is that whenever I click the login button, the method executes twice instead of once. This behavior is puzzling to me. There's also a forgot password button beside it, using the same template but with a different bound function to the click directive. Surprisingly, when I click on the forgot password link, it still runs the login() function, but only once this time.

For reference, here's a screenshot of my login form layout:

https://i.sstatic.net/eBVkx.png

Below you'll find snippets of my code for insight:

button.component.ts

import { Component, ViewEncapsulation, Input, Output, OnInit, EventEmitter } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'app-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ButtonComponent implements OnInit{

  @Input() group: FormGroup;
  @Input() type: string;
  @Input() description: string;
  @Input() class: string;
  @Output() callFunction = new EventEmitter();

  constructor(){ }

  ngOnInit(){
    this.group = new FormGroup({});
  }

  onClick(event){
    this.callFunction.emit(event);
  }

}

button.component.html

<div [formGroup]="group">
  <button [type]="type" [class]="class" (click)="onClick($event)">{{ description }}</button>
</div>

login.component.ts

import { Component, OnInit, ViewChild, ViewContainerRef, AfterContentInit, ComponentFactoryResolver } from '@angular/core';
// Remaining code omitted for brevity

login.component.html

<div class="app-body">
  // Remaining HTML code snippet omitted 
</div>

SIDE NOTE: I am encountering an issue where CoreUI styles are not being correctly applied to child components like text fields and buttons. These components are inheriting plain bootstrap styles instead. Any assistance with resolving this problem would be greatly appreciated.

Answer №1

Within your form, there is a submit callback set for login()

<form [formGroup]="userForm" (submit)="login()">

The issue arises from the submit button's callback function buildLoginButton, which in turn calls login().

To fix this problem, you should structure your form in the following way:

<form [formGroup]="userForm">

Answer №2

The button you are using has a type of "text", which is not a valid value. The correct values should be "button|submit|reset" (refer to documentation for the "type" attribute)

Currently, it is defaulting to "submit", triggering the submit callback function (login()).

To resolve this issue, change the button type to button.

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

export keyword in TypeScript disassociates the interface implementation from a class

Currently, I am facing an issue with my files. I have a .ts file that contains a class declaration, and a .d.ts file that contains an interface declaration. An example from the .ts file: class A { constructor() { this.name = "Jo"; } } A ...

Tips for simulating a configuration dependency using Proxyquire in TypeScript

Within my codebase, there exists a config.ts file that contains the following method: // Config interface is utilized to specify expected values export default function getConfig(): Config { return {amount: 50} } In a specific class located at ../src ...

Issue with index creation using the @index decorator in Typegoose with NestJS and MongoDB

Encountering an issue with typegoose. Trying to create a 2dsphere index on the property geoLocation of model SP. Utilized the typegoose decorator @index but it's not functioning and not throwing any errors. Uncertain about how typegoose handles this s ...

How to stop a form from being cleared when a button is clicked in Angular 2

Within my form, there is a button that allows users to add an item to the object's array. <form (ngSubmit)="submit()" #myForm="myForm" class="form-horizontal" style="direction: ltr"> <div class="row"> <div class="col-md-6"> ...

Ways to address the issue of duplicated names in input fields

Currently, I am utilizing a React Hook Form hook known as useFieldsArray. This hook is responsible for rendering an array of fields, each containing an object with the data that will be transmitted via the input. One interesting feature is the ability to ...

Tips for dynamically binding the image source in Angular based on certain conditions

Hi, I'm receiving a response from an API that only includes a name but no image source. However, I have a collection of images in my local images folder. <div class="left"> <img src="{{ record.imgSrc }}" alt="{ ...

Ionic Troubleshoot: Issue with Reading Property

Encountering an error: A TypeError occurs: Cannot Read Property of "username" of Undefined The HTML code responsible for the error is as follows: <ion-content padding style="text-align: center; margin-top: 35px"> <form (ngSubmit)="logFor ...

Tips for modifying a static property in TypeScript

I am currently developing a class that wraps around a WebSocket to function as an ingestor. After successfully setting up the ingestion process and passing a function to the class instance for processing incoming messages, I encountered an issue. I need t ...

Modifying ngModel will result in the corresponding ngModel in another Angular2 browser being updated

I'm a novice when it comes to Angular 2 and I'm currently working on a project using npm to run the application. At the moment, I have it hosted on localhost with port 3000. The issue I'm facing in my project is that when I open the same ...

What is the best way to distribute a Typescript module across multiple projects without having to duplicate it each time?

I currently have two folders named shared and project1 within a larger folder called node. I am in the process of working on multiple Node projects that are independent and are all located within the node folder. Within the shared folder, there is an inde ...

Tips on updating the datepicker format to be dd/mm/yyyy in ngbdatepicker

I am currently using ng-bootstrap for a datepicker and need to change the date format from yyyy/mm/dd to dd/mm/yyyy. I have tried to make this adjustment but haven't had success. If anyone has suggestions on how to accomplish this, please help. Here ...

Launching a new tab with a specific URL using React

I'm attempting to create a function that opens a new tab with the URL stored in item.url. The issue is, the item.url property is provided by the client, not by me. Therefore, I can't guarantee whether it begins with https:// or http://. For insta ...

Authentication through Auth0 login

After successfully registering a user in Auth0 for login purposes (found in the Users section of the dashboard), I implemented the following code to authenticate the user using an HTML form with username and password fields: public login(username: string, ...

:host-selector for Angular Material dialog

I am currently working with a dialog component provided by angular-material and I need to customize the appearance of the popup dialog. I am aware that there is some support for styling through the component generation: let dialogRef = dialog.open(MyDi ...

Changes are made to the Angular template-driven form after certain controls have been added or removed

Within a fieldset, there exists a flexible number of 'select' drop down lists, accompanied by a button after each one (except the last one) to remove it. Upon selecting an option from the last select control, a new select control is dynamically a ...

Issue arises with data types while utilizing the shadcn FormField element

Error: Type 'foo' cannot be assigned to type 'InputProps' Currently, I am attempting to create a reusable component named TextInput that encapsulates the Shadcn FormField component. The documentation specifies the need to pass a "field ...

Encountering an issue on Safari: WeakMap variable not found in .NET Core 1.1.0 and Angular 2 application

I recently deployed a .NET Core 1.1.0 + Angular 2 + Typescript app on ASPHostPortal and encountered an issue while accessing it from Safari. The console showed the following exception: Can't find variable:WeakMap This caused the site to not load p ...

Angular does not propagate validation to custom form control ng-select

In my Angular 9 application, I am utilizing Reactive Forms with a Custom Form Control. I have enclosed my ng-select control within the Custom Form Control. However, I am facing an issue with validation. Even though I have set the formControl to be requir ...

Branching tests within a method in Angular

Recently, I've implemented a method in my TypeScript file that contains 3 different branches. Now, as I'm working with Angular and Jasmine, I find myself wondering - how can I effectively test all of these branches? getAges(ages: Ages) { if ...

tips for concealing a row in the mui data grid

I am working on a data grid using MUI and I have a specific requirement to hide certain rows based on a condition in one of the columns. The issue is that while there are props available for hiding columns, such as hide there doesn't seem to be an eq ...