The functionality of the $setPristine() method in Angular 1.2.1 does not seem to be functioning correctly as expected

I'm facing an issue while attempting to reset a textbox using the $setPristine function in AngularJS. Despite my efforts, I am not seeing the intended behavior.

This is how my form is structured:

<form name="addInviteForm" ng-controller="InviteCtrl" ng-submit="sendInvitation(userEmail)">

      Pristine? {{addInviteForm.$pristine}}

      <!-- email input -->
      <div>
        <input type="email" name="email" ng-model="userEmail" placeholder="Enter email here"  class="line-item-input see" required>
        <span class="error" ng-show="addInviteForm.email.$error.email" style="color:red">Invalid Email</span>
      </div>

      <!-- submit button -->
      <input type="submit" name="send" class="btn btn-success center" value="Send Invitation">
</form>

Here is the corresponding code snippet from my controller:

$scope.sendInvitation = function(userEmail) {

        // carry out necessary operations ...

        // despite trying this approach, it doesn't seem to be effective
        $scope.addInviteForm.$setPristine();
    };

While the form initially indicates that $pristine is set to true upon entry, and then changes to false when data is entered in the text box, after submission the form reverts back to showing $pristine as true .... however, the text-box still retains its previous value.

What could possibly be missing in my implementation?

Answer №1

$setPristine does not reset the values of the controls in the form:

According to the documentation:

This function sets the form back to its original pristine state.

By calling this method, you can remove the 'ng-dirty' class and revert the form to its pristine state (ng-pristine class). This action will also be applied to all controls within the form.

Resetting a form to a pristine state is particularly helpful when we need to 'reset' a form after saving or submitting it.

As mentioned in the description above, invoking $setPristine only changes the form's state (and therefore resets the CSS styles applied to each control within the form).

If you wish to clear the values of every control, you will need to manually loop through each one in your code.

Check out this plunker example to see how $setPristine works.

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

Tips for solving window dependency issues when executing code in node.js

I am in the process of expanding my browser-side library's capabilities to work with node.js. To achieve this, I have implemented the Universal Module Definition (UMD) pattern. While it successfully works with AMD implementation and <script> tag ...

Choosing between Ajax and Websockets for sending small amounts of dataOR

I am currently developing a chat website that utilizes Websockets (Socket.io) to facilitate communication between users and the server. While Websockets are essential for sending and receiving messages in real-time, I am facing a challenge when it comes to ...

Utilize drag and drop functionality to interact with an HTML object element

I am struggling with a div that contains a PDF object and draggable text: <html> <head> <script> function allowDrop(ev) { ev.preventDefault(); } function drop(ev) { alert("DROP"); } </script> </head> <body> <di ...

Tips for enabling CORS in react.js

Currently working on a project with Reactjs and utilizing an API via AJAX in JavaScript. Any suggestions on resolving this issue? I had been relying on CORS tools, but now it seems enabling CORS is necessary. ...

Guide on Creating a Popup Window When the User's Cursor Moves Beyond the Webpage Boundary

Imagine you're browsing a webpage and as you move your mouse towards the "back" button, a window pops up within the webpage area, displaying important information. This is a clever way to grab the user's attention before they leave the website. B ...

Incorporating HTML into the Ajax Response

I recently encountered a strange issue with a service that returns an HTML page as the AJAX response. This page contains a form that is automatically triggered by scripts within the page, resulting in a POST request being sent when the page is rendered. My ...

Adjust the field of view of the camera in ThreeJS

I'm currently working on adjusting the field of vision for a camera without having to create a new one. So far, I've successfully achieved this for the position using the following code: camera.position.set() Now, I'd like to implement a s ...

Troubleshooting my code: The mystery of why Google Map directions won't cooperate!

Can anyone help me figure out why my code for drawing a path between multiple points on a map isn't working? I've tried the code below, but it doesn't draw any paths. What could be causing this issue and how can I solve it? var myTrip = [] ...

Using JQuery to delete an item by clicking on the delete button and then clicking on the item to remove it

I am currently using jQuery to dynamically create items on an HTML canvas for users to drag around and create drawings in a style similar to Microsoft Visio. However, I am struggling with how to remove these items once they have been created. While I know ...

Deactivate the button in the final <td> of a table generated using a loop

I have three different components [Button, AppTable, Contact]. The button component is called with a v-for loop to iterate through other items. I am trying to disable the button within the last item when there is only one generated. Below is the code for ...

Delayed response of text effects in JQuery on page load

Within my rails app, I have the following code snippet: window.onload = -> $("#mycontainer").typewriter() $("#div1").fadeIn("slow") This code snippet interacts with the following block of content: <blockquote class="pull-left"> < ...

What is the best way to incorporate JavaScript in a repeater?

I am currently working on an asp.net project that involves using a repeater. <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <asp:label ID="lblexp" runat="server" Text="05/11/1 ...

Selecting a value will cause other blocks to vanish

How do I hide other filter buttons when I select a value? Check out my code snippet below: const FilterBlock = props => { const { filterApi, filterState, filterFrontendInput, group, items, name, ...

Tips for concealing XHR Requests within a react-based single page application

Is there a way to hide the endpoint visible in Chrome's devtools under the network tab when data is fetched in React? Can server-side rendering solve this issue? ...

Ways to include a fresh value using an existing key in a document

Is there a way to keep track of multiple categories and their corresponding costs in a category object? For example, if I add the products: $50 category to the user in my code, it displays 'categories': { products: '50$' } in the consol ...

Leveraging Vue slots to display a component inside another component

I am currently working on creating a base modal component that can be re-used, utilizing slots to insert the modal content. The issue I am facing is that when the modal is triggered by clicking a button, it displays without any content present. How can I e ...

What is the best way to include list items and store them in local storage while developing a Phonegap app?

I've been working on an app that tracks the hours worked and earnings at work. Currently, I have the following code: localStorage.cachedPay = madeamount.toFixed(2); localStorage.cachedHours = houramount; $madeamount.html(localStorage.cachedPay); $hou ...

What is the best way to conceal the dt tag when the dd tag contains no value

Is there a way to hide the "Subject" if the "subject_title" field is empty? <dt class="col-sm-6 text-dark" >Subject</dt> <dd class="col-sm-6">{{$course_dtl->subject_title }}</dd> For example, I would li ...

Managing simultaneous asynchronous updates to the local state

There is a scenario where a series of asynchronous calls are made that read from a local state S, perform certain computations based on its current value, and return an updated value of the local state S'. All these operations occur at runtime, with ...

Ways to maximize the usefulness of input[type=number] for obtaining authentic value

When using input[type=number], I noticed that Chrome allows users to type not only numbers, but also special characters like the minus sign (-) for negative numbers. Surprisingly, it even permits multiple instances of these characters, such as ".....---888 ...