angularjs - $setPristine() 不工作。

标签 angularjs

**** 注意我删掉了一些其他功能,以便可以更快地阅读代码。我无法清除表格。当我单击具有取消功能的按钮时。我以为我可以设置一个默认表单,但这并没有什么不同。

<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>  

这是我的 javascript,我正在尝试使用带有 ng-click 的按钮命令将我的表单设置为 $setPristine。我虽然设置默认表单会有所帮助,但提交时没有任何 react
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');
        }

最佳答案

我认为你用错了。

$setPristine:

“可以调用此方法来删除 'ng-dirty' 类并将表单设置为其原始状态(ng-pristine 类)。此方法还将传播到此表单中包含的所有控件。”

所以这只会清除类而不是 $scope 变量。
您确实重置了 $scope.user 变量,可以说:

添加用户。'在 Html 中的每个模型前面

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

并在您的 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');
}

对此:
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');
}

关于angularjs - $setPristine() 不工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27191744/

相关文章:

angularjs - Angular 绑定(bind)在数据属性中不起作用

javascript - 错误 : [ng:areq] Argument is not a function, 未定义

angularjs - 重新使用 Controller /工厂时防止多次 ajax 调用

javascript - ng-repeat track by 不起作用 : slow and still generating $$hashKey

javascript - NG-keypress 防止特定 charCode 的默认值

javascript - 如何在javascript中调用递归函数

javascript - 未定义的 Angular 2 的“reflectionCapability”

javascript - 为什么控制台中不显示 html 中的 AngularJS 错误?

javascript - 更新指令内的 attrs 值 - 如何在 AngularJS 中执行此操作

javascript - Angular 2 "this"无法访问嵌套函数中的全局变量