Angular JS表单重置,Pristine无法正常工作

时间:2016-08-06 10:46:06

标签: javascript angularjs

我有登录表格, 我在表单提交后清除了表单,并将表单设置为Pristine模式。 但错误信息仍然存在。

这是我的代码,

<form name="loginForm" ng-submit="loginForm.$valid && login(user)" novalidate style="position: relative">
    <div class="log-input-frm mdl-textfield mdl-js-textfield mdl-textfield--floating-label textfield-demo">
        <div class="col-sm-12 f_name">
            <input ng-model="user.username" name="username" type="email" placeholder="Email address" required="">
            <div ng-show="loginForm.$submitted || loginForm.username.$touched" ng-model="loginForm.username">
                <span ng-show="loginForm.username.$error.required" class="text-danger">Please enter email address.</span>
                <span ng-show="loginForm.username.$error.email" class="text-danger">Please enter valid email address.</span>
            </div>
        </div>
    </div>
    <div class="log-input-frm mdl-textfield mdl-js-textfield mdl-textfield--floating-label textfield-demo">
        <div class="col-sm-12 f_name">
            <input ng-model="user.password" name="password" type="password" placeholder="Password" required="">
            <div ng-show="loginForm.$submitted || loginForm.password.$touched" ng-model="loginForm.password">
                <span ng-show="loginForm.password.$error.required" class="text-danger">Please enter password.</span>
            </div>
        </div>
    </div>
    <div class="buttons">
        <span class="signup pull-left">
            <a ui-sref="forgotpassword">Forgot password</a>
        </span>
        <div class="link">
            <div class="link" style="padding-bottom:0;">
                <md-button type="submit" class="md-raised primary" > Login </md-button>
            </div>
        </div>
    </div>
</form>

控制器代码,用于重置表单并设置为原始模式。

$scope.loginForm.$setPristine();
$scope.loginForm.$setUntouched();
$scope.user = {};

我哪里错了???

1 个答案:

答案 0 :(得分:0)

我通过使用超时功能解决了我的问题

$scope.user = {};
$timeout(function () {
    $scope.loginForm.$setPristine();
    $scope.loginForm.$setUntouched();
    $scope.loginForm.$submitted = false;
});