小编noe*_*oel的帖子

有没有办法重置Angular中的$触摸?

我正在尝试为我的表单实现"重置"按钮.我的一些文本字段使用$ 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>
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

我的resetForm:

$scope.resetForm = function () {
        $scope.donationForm.$setPristine();
        $scope.model = '';

        $scope.donation = donationService.defaultDonation;
        $scope.editableDonation = angular.copy($scope.donation);
    };
Run Code Online (Sandbox Code Playgroud)

http://plnkr.co/edit/Oa91ZkTatsddSA4oujQK

angularjs

8
推荐指数
1
解决办法
8284
查看次数

标签 统计

angularjs ×1