When using Angular forms, the password or username may be duplicated if entered twice when pressing the

I am currently working on a basic username and password form using Angular. Here's the template I have:

<label class="welcome-content">Username:</label>
<input #userName type="text" id="txtLoginUsername" (keyup.enter)="loginUser(userName.value)"
                                                                     (blur)="loginUser(userName); loginUser.value='' ">
<label class="welcome-content">Password:</label>
<input #userPass type="password" id="txtLoginPassword" (keyup.enter)="loginPass(userPass.value)"
                                                                     (blur)="loginPass(userPass); loginPass.value='' ">
<button class="linkButton welcome-content"  id="lnkLogin" (click)="loginUser(userName.value); loginPass(userPass.value)">Login</button>

As for the controller:

userName: string = null;
userPass: string = null;

loginUser(userName): void {
    this.userName = userName;
    console.log(this.userName);
  }

  loginPass(userPass): void {
    this.userPass = userPass;
    console.log(this.userPass);
  }

Everything works fine when clicking the button, but pressing enter in the username field results in the username being output twice, and the same occurs for the password field if enter is pressed there. How can I ensure that the values are only input once?

Answer №1

Here is the code snippet:

<input #userName type="text" id="txtLoginUsername" 
     (keyup.enter)="loginUser(userName.value)"
     (blur)="loginUser(userName); loginUser.value='' ">

Upon reviewing the provided code, it seems like the intention is to only log the user in when they specifically click the Login button. Therefore, it would be advisable to remove the loginUser method from both input boxes.

<label class="welcome-content">Username:</label>
<input #userName type="text" id="txtLoginUsername" >
<label class="welcome-content">Password:</label>
<input #userPass type="password" id="txtLoginPassword">

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

Error in Angular 2: Trying to access a property that is undefined - 'Symbol(Symbol.iterator)'

It appears that the error I am encountering is related to how I handle data when returning it. I have been unable to pinpoint where my return statement may be missing or if that is even the root cause of the issue... The error occurs when deleting somethin ...

choosing between different options within Angular reactive forms

I am attempting to create a select element with one option for each item in my classes array. Here is the TypeScript file: @Component({ selector: 'app-create-deck', templateUrl: './create-deck.component.html', styleUrls: [' ...

"assurance of not issuing a return value in the correct order

Hey everyone, I'm a newcomer to the world of Ionic! I'm looking to pull data from pouch-db in the background. After some research, it seems like promises are the way to go. My goal is to have my console logs display in the order: 1, 2, and then ...

Angular 9 TestBed RouterTestingModule: Exploring the router.url Readonly Property

While transitioning from Angular 8 to Angular 10 in stages, I encountered an issue when upgrading to version 9. All of my TestBed.get(Router).url calls started throwing errors because the property had become read-only. For instance, the code TestBed.get(R ...

Turn off the background graphic in chartjs

Greetings all! I'm currently facing an issue with removing the hashtag background of a line chart. Is there any method to disable or remove it? I am utilizing chartjs in my Ionic 2 app. Your assistance is greatly appreciated. Below is the code for my ...

Switching Angular fxLayout from row to column based on a conditional statementHere is an explanation of how to

Visit this link for more information Is there a way to set direction based on a specific value? <div if(value) fxLayout='row' else fxLayout='column'> ...

How can I assign two different colors based on the type in Typescript?

I am configuring a color property based on the nature of the display. colorStyle: { textAlign: "center", backgroundColor: "transparent", color: (theme.colors.BaseColor.Red as any).Red4, } The cu ...

Error detected in Deno project's tsconfig.json file, spreading into other project files - yet code executes without issues?

I am working on a Deno project and need to utilize the ES2019 flatMap() method on an array. To do this, I have created a tsconfig.json file with the following configuration: { "compilerOptions": { "target": "es5", ...

Tips on preventing the initial undefined subscription in JavaScript when using RxJS

I am having trouble subscribing to an object that I receive from the server. The code initially returns nothing. Here is the subscription code: ngOnInit() { this.dataService.getEvents() .subscribe( (events) => { this.events = events; ...

Unusual behavior of Typescript with Storybook's addon-docs

I'm trying to integrate storybook addon-docs into my TypeScript React project. Everything seems to be almost working, but I've noticed that the file name is affecting how the props type table gets rendered. Here is my file structure: src - Butto ...

Executing an observable only when a certain variable is in a specific state at the class level

Is there a way to conditionally return an observable in a clean manner, based on a predefined condition? Let's look at the scenario below: isEditing = false; // <---- Variable defined at class level return this.usersService.fetchInitialCreateDat ...

When trying to run ionic serve, I encountered the following error: "[ERROR] ng has unexpectedly closed with an exit code of 127."

My attempt to launch an ionic app on my Mac has hit a roadblock. While running npm install for the dependencies, everything goes smoothly without any issues. However, when I try to run 'ionic serve' or 'ionic s', an error crops up: [ng] ...

What is the method for implementing a custom layout for the items within a Select component?

I want to customize the options of a Select component by adding HTML elements. Here is an example: <mat-select [(ngModel)]="items"> <mat-option *ngFor="let item of ($items | async)" [value]="item.id"> <span>{{item.name}}</span&g ...

Load components dynamically and place them in a flexible position based on the context

UPDATE (After gaining a better understanding of the issue): I'm trying to display a component based on where the user clicks (specifically, which table row). Using ng2-smart-table, I've encountered an issue where there isn't a suitable sele ...

Is the use of a Backend-for-Frontend necessary when working with a framework such as Angular?

For my frontend application, I'm utilizing Angular and connecting to an existing backend service for data retrieval. This backend service is established as a legacy system that I don't have control over. To enhance security, I've integrated ...

ngif directive does not function properly when subscribe is utilized in ngOnInit

this is the unique component code import { Component, OnInit } from '@angular/core'; import { Subscription } from 'rxjs'; //import { Item } from '../item/item.model'; import { CartItem } from './cart-item.model'; imp ...

My Angular custom libraries are having issues with the typing paths. Can anyone help me troubleshoot what I might be doing

After successfully creating my first custom Angular library using ng-packagr, I encountered an issue where the built library contained relative paths specific to my local machine. This posed a problem as these paths would not work for anyone else trying to ...

Accessing the state from a child functional component and then adding it to an array of objects in the parent component

I'm facing a challenge with a parent component that needs to manage the data of its child components stored in an array of objects. My goal is to add new child components and maintain their information within the parent's state as an array of obj ...

Ignore any information in NestJS that is not included in the data transfer object

In my NestJS controller, I have defined a route for updating locality information. The structure of the controller method is as follows: @Put('/:id') updateLocalityInfo( @Query('type') type: string, @Body() data: EditLocalityD ...

Why are the icon pictures not displaying in Angular's mat-icon-button?

Recently, I stumbled upon a snippet of code in an Angular project that caught my eye. Naturally, I decided to incorporate it into my own program: <div style="text-align:center"> <a mat-icon-button class="btn-google-plus" href="http://google.com ...