0

我有登录表单,如果用户名或密码错误,我会清除模型,当我提交表单时模型值被清除,但显示文本框验证消息。这是我的代码。

登录.html

<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.user.password = null;
                    $scope.message = 'Invalid email address or password.';
                    $mdToast.show(
                            $mdToast.simple()
                            .content($scope.message)
                            .position($scope.getToastPosition())
                            .hideDelay(3000)
                            );
                    blockUI.stop();
4

1 回答 1

0
var ORIGINAL_DATA = $scope.user;

重置时:

$scope.user = angular.copy(ORIGINAL_DATA);
$scope.loginForm.$setPristine();

表单控制器的Angular文档

编辑:

试试这个最简单的:

<form name="loginForm" novalidate ng-submit="loginForm.$valid && login.submit()">

无需控制器检查。

于 2016-08-06T09:40:04.053 回答