Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$setPristine() not working.

Tags:

angularjs

**** Note I cut out some of my other functions so that the code could be read through faster. I cannot clear the form. when i click the button with the cancel function. I thought I could set a default form, but this doesn't make a difference.

<form  name="myForm" novalidate ng-submit="submit()"> 
    <table class="mealCost"> 

        <!-- descriptions -->
        <tr> 
            <td> Base Meal Price: </td>
            <td><input type="number" name="price" ng-model="mealPrice" required></td>
        </tr>
        <!-- waiter puts in the info -->
        <tr> 
            <td> Tax Rate: % </td>
            <td><input type="number" step="0.01" name="tax" ng-model="mealTax" required></td>

        </tr>

        <tr> 
            <td> Tip Percentage: % </td>
            <td><input type="number"  name="tip" step="0.01" ng-model="tipPercent" required></td>

        </tr>

    </table>

    <p class="userResponse"> 
    <input type="submit" value="Submit"> 
    <!-- <input id="cancel" type="submit" value="Cancel" ng-submit="cancel(original)"> -->
    <button ng-click="cancel()">Start Over</button>
    </p>

</form>  

Here is my javascript I am trying to set my form to $setPristine using the button command wiht ng-click. I though that setting a default form would help but nothing happens on submit

var app = angular.module('myApp',[]).
    controller('costController', function($scope) {
        // $scope.ready= false;
        $scope.mealPrice ="" ;
        $scope.mealTax = 0.05;
        $scope.tipPercent =0.05; 
        //  possibly could do 

        var defaultForm={
            price: "",
            tax: "",
            tip:""
        }

$scope.cancel = function() {
            $scope.myForm.$setPristine();
            $scope.user = angular.copy(defaultForm);
            console.log('empty');
        }
like image 226
Winnemucca Avatar asked Nov 28 '14 15:11

Winnemucca


People also ask

What is$ setPristine?

$setPristine(); Sets the form to its pristine state. This method sets the form's $pristine state to true, the $dirty state to false, removes the ng-dirty class and adds the ng-pristine class.

How to reset form AngularJS?

To reset the form data use following code : $scope. resetEmployeeData = function() { $scope. employeeCred. userName = ''; $scope.

What is$ dirty in AngularJS?

$dirty means the user has changed the input value, $invalid means the address itself is invalid. Therefore the error is only shown if the user has actively changed the input value to either an empty or invalid value.

What is form controller?

A form controller is triggered either upon the form's submission or on adding or updating input in a form field. When a user adds or updates an input value in a form field or clicks the submit or cancel button in the form, the form function associated with this function is triggered to execute the intended action.


3 Answers

I think you're using it wrong.

$setPristine:

"This method can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). This method will also propagate to all the controls contained in this form."

So this only clears classes but not the $scope variables. You do reset a $scope.user variable, could say:

Add 'user.' in front of every model in the Html

ng-model="user.tipPercent"
ng-model="user.mealTax"
ng-model="user.mealPrice"

And replace this in your JS:

// $scope.ready= false;
$scope.mealPrice ="" ;
$scope.mealTax = 0.05;
$scope.tipPercent =0.05; 
//  possibly could do 

var defaultForm={
    price: "",
    tax: "",
    tip:""
}

$scope.cancel = function() {
    $scope.myForm.$setPristine();
    $scope.user = angular.copy(defaultForm);
    console.log('empty');
}

to this:

var defaultForm = {
    mealPrice : "",
    mealTax : 0.05,
    tipPercent : 0.05
}

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

$scope.cancel = function () {
    $scope.myForm.$setPristine();
    $scope.user = angular.copy(defaultForm);
    console.log('empty');
}
like image 139
Jason van der Zeeuw Avatar answered Oct 17 '22 06:10

Jason van der Zeeuw


$scope.master = {};

$scope.reset = function(form) {

      if (form) {
          form.$setPristine();
           $scope.user = angular.copy($scope.master);
         form.$setUntouched();
      }
like image 23
Blisha Avatar answered Oct 17 '22 07:10

Blisha


This would work if the ng-model were bound to $scope.user.price, $scope.user.tax and $scope.user.tip. However, they are bound to $scope.price, $scope.tax and $scope.tip.

Setting the form $pristine only marks the values as not modified by user. It doesn't actually change the values.

Solution A:

Bind the models to user.* and replace

$scope.mealPrice = '';
$scope.mealTax = 0.05;
$scope.tipPercent = 0.05; 

with

$scope.user = {
    mealPrice: '',
    mealTax: 0.05,
    tipPercent: 0.05
};

Solution B:

Replace:

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

with

$scope.mealPrice = defaultFrom.mealPrice;
$scope.mealTax = defaultFrom.mealTax;
$scope.tipPercent = defaultFrom.tipPercent; 
like image 43
Sulthan Avatar answered Oct 17 '22 06:10

Sulthan