1

HTML :

<form name="ContactForm" novalidate >
    <div class="list">
      <label class="item item-input">
        <input type="text" placeholder="First Name" ng-model="contact.firstName" name="uFirstName" required="" autocomplete="off"/>
      </label>
        <br>
        <div ng-show="ContactForm.$submitted || ContactForm.uFirstName.$touched" ng-hide="hidespan">
        <span class="error" ng-show="ContactForm.uFirstName.$error.required">Tell us your First Name.</span>
        <span class="error" ng-show="ContactForm.uFirstName.$error.text">This is not a valid First Name.</span>
        </div> 
        </div>
    <button class="button button-block button-assertive" ng-click="ContactForm.$valid && contactReq(contact)">
      Submit
    </button> 
    </form>

JS

var defaultForm = {
        firstName : "",
        lastName : "",
        email : "",
        message : ""
    }

    $scope.contact = angular.copy(defaultForm);

    $scope.contactReq = function(contact){
        $scope.ContactForm.$setPristine();
        $scope.contact = angular.copy(defaultForm);
        console.log('empty');
    }

I am using AngularJS v1.4.3 for phonegap development. I want to reset the form after clicking the submit button. But when i am doing set Pristine i am getting the following error: "ERROR: Error: undefined is not an object (evaluating '$scope.ContactForm.$setPristine')" I tried many links related same but nothing is working for me.

Any help is highly appreciated.

13

2 Answers 2

0

ContactForm should be on the scope. Just a guess: Copy the values of the default form to the model before you call setPristine()

Sign up to request clarification or add additional context in comments.

Comments

0

I had a strange issue using Safari 9.01. My form inputs were not editable after I switched from my Login to Registration page.

So I tried using the $setPristine() method and experienced the same problem. I found that putting a timeout in the reset function allowed me to access the form.

You could try:

    $scope.contactReq = function(contact){
        $timeout(function(){
            $scope.ContactForm.$setPristine();
            $scope.contact = angular.copy(defaultForm);
            console.log('empty');
        },10);
    }

If that doesn't work try using this.ContactForm.$setPristine(); instead of $scope.ContactForm.$setPristine();

My problem ultimately ended up being a issue with the way I was using ui-router to change between my views.

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.