Angular JS Form's Pristine feature is malfunctioning when attempting to reset

I implemented a login form on my website. After submitting the form, I clear it and set it to Pristine mode. However, the error message still persists.

Below is the code for my form:

<form name="loginForm" ng-submit="loginForm.$valid && login(user)" novalidate style="position: relative">
    <div class="log-input-frm mdl-textfield mdl-js-textfield mdl-textfield--floating-label textfield-demo">
        <div class="col-sm-12 f_name">
            <input ng-model="user.username" name="username" type="email" placeholder="Email address" required="">
            <div ng-show="loginForm.$submitted || loginForm.username.$touched" ng-model="loginForm.username">
                <span ng-show="loginForm.username.$error.required" class="text-danger">Please enter email address.</span>
                <span ng-show="loginForm.username.$error.email" class="text-danger">Please enter valid email address.</span>
            </div>
        </div>
    </div>
    <div class="log-input-frm mdl-textfield mdl-js-textfield mdl-textfield--floating-label textfield-demo">
        <div class="col-sm-12 f_name">
            <input ng-model="user.password" name="password" type="password" placeholder="Password" required="">
            <div ng-show="loginForm.$submitted || loginForm.password.$touched" ng-model="loginForm.password">
                <span ng-show="loginForm.password.$error.required" class="text-danger">Please enter password.</span>
            </div>
        </div>
    </div>
    <div class="buttons">
        <span class="signup pull-left">
            <a ui-sref="forgotpassword">Forgot password</a>
        </span>
        <div class="link">
            <div class="link" style="padding-bottom:0;">
                <md-button type="submit" class="md-raised primary" > Login </md-button>
            </div>
        </div>
    </div>
</form>

I also have controller code to reset the form and set it back to pristine mode:

$scope.loginForm.$setPristine();
$scope.loginForm.$setUntouched();
$scope.user = {};

Despite this, the error message still remains visible. Can you help me figure out what went wrong?

Answer №1

The issue was successfully resolved by implementing a timeout function.

$scope.user = {};
$timeout(function () {
    $scope.loginForm.$setPristine();
    $scope.loginForm.$setUntouched();
    $scope.loginForm.$submitted = false;
});

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

Eliminate the need for index.html in the URL when using MVC WebApi

I am currently utilizing Angular for the front end and have an index.html page as the starting point. On the main page, the URL appears as follows: localhost:588/index.html#/ I desire it to appear as: localhost:3478/#/ or simply localhost:3478/ The iss ...

When using sequential jQuery 'pages', an error referencing the third frame occurs

I am new to using javascript/jquery and have been experimenting with the w3schools tutorials and jquery documentation. I created a page where user input is accepted and javascript prints output based on that input. I tried modifying it to work sequentially ...

What is the correct way to integrate the Ant Design library with Next.js for seamless server-side rendering?

I duplicated the official Next.js example using Ant Design at this link: https://github.com/vercel/next.js/tree/canary/examples/with-ant-design After cloning, I proceeded with npm install to install all dependencies. Then, I ran npm run dev to check if ev ...

Issue: The module "node:util" could not be located while attempting to utilize the "sharp" tool

Upon adding sharp to my Node.js application and attempting to use it, I encountered the following error: /Users/username/Documents/GitHub/Synto-BE/node_modules/sharp/lib/constructor.js:1 Error: Cannot find module 'node:util' Require stack: - /Use ...

Firestore version 9 - Retrieve nested collection depending on a string being present in an array

Working on a new chat application here. It has been a while since I last used Firestore. I am currently using v9 and facing an issue with retrieving the nested "messages" collection when the "users" array in the document contains a specific ID. I have man ...

Is it possible to select options in AngularJS based on certain criteria?

I created an AngularJS select menu that is currently functioning correctly: <select name="s1" id="s1" multiple="multiple" data-native-menu="false" data-role="none" ng-model="userlistSelect" ng-options="u.userId as u.fi ...

"Enhancing User Interaction with jQuery Hover State Dropdown Menus

Here's my issue: I've created a drop-down menu and I want the text color to change when hovering over the menu. Additionally, I'd like the hover state to remain active when hovering over the submenu. Currently, I'm using this code: $( ...

Guide to assigning a value to a model in AngularJS by utilizing a select input with an array of objects

I'm new to AngularJS and I've encountered a challenge that requires an elegant solution. My application receives a list from the server, which is used as the data source for the select tag's options. Let's assume this list represents a ...

Ways to eliminate models that are not currently connected to the DOM

Within my form, I have implemented logic using ng-if to determine which elements are displayed based on user selections. For instance, if a user selects USA as the country, the state drop down will be shown as it was conditioned upon an ng-if for the count ...

Using Node.js to extract text from a local file on Azure using OCR technology

I recently started using the Azure OCR Service to extract text from images (https://learn.microsoft.com/de-de/azure/cognitive-services/Computer-vision/quickstarts/javascript#OCR). While things have been going smoothly so far with uploaded images, I am now ...

sending information from a PHP form to a JavaScript window

Currently, I am in the process of developing a game using javascript and jquery. In this game, when a player interacts with another character, it triggers the opening of text from an external file using the window.open('') function. At the start ...

``How can I extract the last string in Vue.js when working with string processing?

I've been working with strings in vuejs and currently have 4 URL strings: https://web-sand.com/product/slug/apple-iphone-13 https://web-sand.com/product/slug/samsung-galaxy https://web-sand.com/product/slug/xiaomi-red https://web-sand.com/product/slug ...

PHP implementation for a static header layout

I am interested in learning how to update content without refreshing the header. I have created a simple example below. Header.php <html> <head> </head> <body> <ul> <li><a href="index.php" ...

Tips for deleting Material Angular CSS codes from the head section of your website

I am currently studying AngularJS and Material Angular Design. However, I have noticed that the Material Design includes CSS codes within the <head> tags. See attached screenshot for reference. Is there a way for me to extract these codes from the h ...

The dichotomy between public and private methods within a Vue.js component

Within a component, I have two functions defined. One is foo(), which is defined within <script>, and the other is fooExported(), which is defined in the body of export default {}. My understanding is that functions inside export default {} can be a ...

What is the best way to select a cell within the same row using jQuery?

I've successfully implemented a table with a single input field and an AJAX script that triggers when the input field value is changed. Everything is functioning as expected. However, I now face the challenge of adding a dynamic date insertion feature ...

Delete dynamic elements from an array once they are no longer present in the DOM

In this code snippet, I have implemented a button that adds a new object to both the document and an array. When you click on each object, it gets removed from the document. However, the challenge here is how can we also remove it from the array? You can ...

How can I combine multiple styles using Material-UI themes in TypeScript?

There are two different styles implementations in my code. The first one is located in global.ts: const globalStyles = (theme: Theme) => { return { g: { marginRight: theme.spacing(40), }, } } export const mergedStyle = (params: any) ...

Exploring the power of QueryTask in ArcGIS JS API 3 for running multiple queries

When using QueryTask in ArcGIS JS Api 3, I encountered a challenge where I needed to execute multiple queries in one call. After referencing the documentation, I realized that this functionality was not directly supported. This is my current implementatio ...

Regular Expression designed specifically for detecting alternative clicks

When using ngpattern for validation, I have encountered an issue where my error message is displaying for a different reason than intended. The error message should only show if the field is empty or contains only special characters. <textarea name="ti ...