我正在尝试为我的表单实现“重置”按钮.我的一些文本字段使用$touch来确定是否显示错误.但是一旦设置了$touch标志,当我重置模型时,$touch不会被重置,因此我的错误消息仍然可见.
<div class="col-sm-4" ng-class="{'has-error' : donationForm.firstName.$invalid && donationForm.firstName.$touched}"> <label for="firstName" class="control-label">First Name</label> <input type="text" id="firstName" name="firstName" class="form-control" ng-model="editableDonation.person.firstName" ng-required="true" ng-maxlength="20" /> <span class="help-block" ng-show="donationForm.firstName.$error.required && donationForm.firstName.$touched"> First Name is required. </span> <span class="help-block" ng-show="donationForm.firstName.$error.maxlength"> First Name is too long (20 max). </span> </div>
有没有办法做到这一点?
我的resetForm:
$scope.resetForm = function () { $scope.donationForm.$setPristine(); $scope.model = ''; $scope.donation = donationService.defaultDonation; $scope.editableDonation = angular.copy($scope.donation); };
DeborahK89指出我可以使用:
$scope.donationForm $setUntouched().