Having difficulty clearing an Angular Material design Form using controller logic

I am struggling with resetting an Angular Material design form that has 3 input fields with the required option. Despite using $setPristine to change the form to its default state after clicking the submit button, error lines continue to appear in red. Is there a solution to remove these errors post submission?

Your help is greatly appreciated.

Access the Codepen Link: http://codepen.io/sateesh2499/pen/pbkjVV

Here's the view:

<div ng-controller="AppCtrl" layout="column" ng-cloak="" class="inputdemoErrors" ng-app="MyApp">

  <form name="careersForm">
        <div class="careersContainer">
            <md-content>
                <div layout-gt-sm="row">
                    <md-input-container class="md-block" flex-gt-sm>
                        <label>Job Title</label>
                        <input type="text" name="jobTitle" ng-model="careers.jobTitle" required>
                    </md-input-container>
                    <md-input-container class="md-block" flex-gt-sm>
                        <label>Job Location</label>
                        <input type="text" name="jobLocation" ng-model="careers.jobLocation" required>
                    </md-input-container>
                    <md-input-container class="md-block" flex-gt-sm>
                        <label>Job Category</label>
                        <input type="text" name="jobCategory" ng-model="careers.jobCategory" required>
                    </md-input-container>
                </div>

            </md-content>
        </div>
        <div class="row text-center">
            <div class="col-sm-12">
                <md-button class="md-raised" style="width: 200px"
                           ng-click="postJob()">Submit</md-button>

            </div>
        </div>
    </form>
Pristine: {{careersForm.$pristine}}
</div>

The Controller Logic:

angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])

.controller('AppCtrl', function($scope) {
  $scope.careers = {};
  $scope.postJob = function(){
    // reset form after successful submission
    $scope.careers = {};
    $scope.careersForm.$setPristine();
  }
});

Answer №1

If you add

$scope.careersForm.$setUntouched();
after calling $setPristine, it should solve the issue.

I have created a fork of your plunker to demonstrate how it works. Check it out here: Plunker here

$scope.postJob = function(){
    // after successful posting
    $scope.careers = {};
    $scope.careersForm.$setPristine();
    $scope.careersForm.$setUntouched();
  }

In any case, I believe your code is correct...so it might be a bug.

I hope this information is helpful to you.

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

Using AngularJS to Retrieve Data via a Factory

While I have come across multiple posts on this topic, I am struggling to adapt them to suit my specific requirements. A GET request is currently being made to a server in order to obtain a JSON response, and it functions perfectly. .controller('Lis ...

How to automatically generate a new row in an Ionic Grid for every 2 items using Angular's ng-repeat

In my app, I am aiming to create a structure using ng-repeat that looks like the following: <div class="row"> <div class="col-50">1</div> <div class="col-50">2</div> </div> <div class="row"> <div class="c ...

What is the best way to implement dynamic generation of Form::date() in Laravel 8?

Is there a way to dynamically generate the Form::date() based on the selection of 1? If 1 is selected, then display the Form::date() under the Form::select(). However, if 0 is selected, then hide the Form::date() in this particular view. For example: Sel ...

Assistance needed in handling a form with the use of $_POST and $PHP_SELF

Let me explain my goal in creating a webpage using HTML and PHP. The webpage will feature multiple forms to gather user inputs for future use. My plan is to reveal each form only after the previous one has been submitted. Here are the 3 forms: First name ...

Modify a section of the "src" parameter in an iframe using jQuery

Seeking to deactivate the autoplay feature on a Vimeo iframe embed within a Drupal 6 website. Unable to modify settings in the embedmedia module, opting to utilize jQuery instead. Here is the original "src" for the Vimeo content: <iframe src="http:// ...

What is the best way to filter out empty arrays when executing a multiple get request in MongoDB containing a mix of strings and numbers?

I am currently working on a solution that involves the following code: export const ProductsByFilter = async (req, res) => { const {a, b, c} = req.query let query = {} if (a) { query.a = a; } if (b) { query.b = b; } if (c) { ...

Is it possible to record a video and save it directly to the server?

When using the Apache Cordova Plugin cordova-plugin-media-capture to capture a video, you may wonder how to send this video to your server and store it. An official example is provided on how to start capturing a video: // capture callback var captureSuc ...

When validating with Sequelize, an error occurred due to one or more columns being undefined:

Hello everyone, I'm facing some issues. Can anyone explain why this.day_number and this.teacher_id are coming up as undefined? 'use strict' module.exports = (sequelize, DataTypes) => { const Teacher = sequelize.models.teachers ...

Effortless pagination across various pages, utilizing diverse CSS selectors

I've integrated simple pagination into my website, but I've encountered a issue. My navigation consists of CSS tabs, each holding a unique pagination component. Here is the jQuery pagination code snippet: $(function(){ var perPage = 8; var open ...

Approaches for transferring a jQuery variable to HTML during a post method invocation

Being a novice in HTML and jquery, I have managed to calculate a value within the jquery section, but now I am struggling with passing this variable to my HTML code. <script> var datetime = new Date(); var res = datetime.toString(); //res is ...

Creating a graph of the cosine function by tracking the movement of the mouse

Objective: Modify the length of the cosine wave based on the horizontal position of the mouse cursor. The key function to focus on is mouseMoveHandler. (function(window,document,undefined){ var canvas = document.getElementById('canvas'), rec ...

Could spell-checking be achieved by utilizing Redis as the dictionary in conjunction with AngularJS?

I recently added a search feature to my website that queries my database (or redis) to retrieve results based on user input. The data being searched is comprised of names rather than common dictionary terms. As a result, I am unable to rely on the built-in ...

Comprehensive route from a one-dimensional array

I have a specific array structure that looks like this : var items = [ [1, 0, 'Apple'], [2, 1, 'Banana'], [3, 0, 'Orange'], [4, 3, 'Grape'], [5, 0, 'Cherry'], [6, 0, 'Mango'], [7, 6, 'Pear&a ...

Problem with sending JSON-encoded arrays to an API

After successfully connecting to an API via Postman, I encountered an issue with my AJAX call. The error message returned is: Object {status: false, message: "Add amenities in JSON string"} The problem seems to be related to the amenities parameter, as ...

Is there a way to transform a string into an array that merges the elements together consecutively?

Imagine having a string formatted like this: const name = "Matt" Now, you want to transform it into an array like the following: nameSearchArr = [ 0: "M", 1: 'Ma', 2: 'Mat', 3: 'Matt ] To work around Firestore ...

My authentication process involves utilizing auth0 for verifying users, coupled with an API designed for creating, reading, updating, and deleting posts which include fields for titles, images, and descriptions. What steps should be

I am currently in the process of developing a react application and have implemented auth0 for user authentication. Prior to integrating auth0, I was sending CRUD requests to my API to create posts without any user authentication in place. Now that I have ...

What are the methods for handling JSON type in a web server?

Hey there! I'm currently working on making an AJAX call from the browser to a web service. The data is being sent as JSON from the browser to the web service. I'm wondering if there is a different way to retrieve it as a string and then deseriali ...

Verify whether a certain key exists within the gun.js file

Suppose I need to verify whether a specific entry exists in the database before adding it. I attempted the following: gun.get('demograph').once((data, key) => { console.log("realtime updates 1:", data); }); However, I only receive ...

A guide on receiving object input within AngularJS

I am working on a form that looks like this: <input type="text" ng-model="gen.name" ng-repeat="gen in movie.genres"> The gen in the movie.genre is an object with two key-value pairs: Object{ id: 24, name : insidious } However, when I submit the ...

Retrieve the content of a particular text input field that is accompanied by a checkbox, and then display this information within an

In this example, the "textbox" is currently empty and requires user input. The checkbox should mirror what is typed in the textbox (type="checkbox" value=written-text-on-profile-textbox) When the checkbox is selected, it should take the text entered in th ...