无法使用" $ setPristine'重置表单

时间:2015-06-26 08:19:45

标签: javascript angularjs ionic

运行以下代码重置表单时出错:

 $scope.saveFormData = function () {
            $scope.testForm.$setPristine();
}

和HTML:

<form name="testForm" >

                <label class="item item-input item-stacked-label">
                    <span class="input-label">Title</span>
                    <input type="text" ng-model="formData.shortDesc" required="">
                </label>
 <button class="button button-block button-positive" type="submit" ng-click="saveFormData()" >Opslaan </button>

</form>

1 个答案:

答案 0 :(得分:2)

它工作正常,这里是JSFiddle,如果表单存在,尝试添加条件,但我不知道你得到了什么错误:

function MyCtrl($scope) {
    $scope.saveFormData = function() {
        if ($scope.testForm) {
           $scope.testForm.$setPristine();
           $scope.formData = {};
        }
    };
}