如何重置表单值而不显示验证错误

时间:2016-09-22 08:22:21

标签: javascript angularjs

表单 - HTML

                                            电子邮件地址:                                                                        电子邮件地址字段是必填项                 太长                 无效的邮件地址                                           密码:                                                                        密码字段是必需的                                           登录                             

登录控制器 - Angular JS

(function () {
    'use strict';
    angular
    .module('app')
    .controller('loginController', loginController);

    loginController.$inject = ['ApiService', '$location', 'AuthenticationService', 'FlashService'];
    function loginController(ApiService, $location, AuthenticationService, FlashService) {
        var vm = this;
        vm.userLogin = userLogin;
        (function initController() {
            // reset login status
            AuthenticationService.ClearCredentials();
        })();

        function userLogin() {
        FlashService.clearNotification();
            ApiService.userLogin(vm.user).then(function (response) {
                if (response.success) {
                    FlashService.Success(response.message); 
                    } else {
                    FlashService.Error(response.message);
                }
                vm.user.$setPristine();
            });
        }
    }   
 })();

1 个答案:

答案 0 :(得分:0)

我认为您还必须设置vm.user = {};vm.user.$setPristine(true);

loginController.$inject = ['ApiService', '$location', 'AuthenticationService', 'FlashService'];
function loginController(ApiService, $location, AuthenticationService, FlashService) {
    var vm = this;
    vm.userLogin = userLogin;
    (function initController() {
        // reset login status
        AuthenticationService.ClearCredentials();
    })();

    function userLogin() {
    FlashService.clearNotification();
        ApiService.userLogin(vm.user).then(function (response) {
            if (response.success) {
                FlashService.Success(response.message); 
                } else {
                FlashService.Error(response.message);
            }
            vm.user = {};
            vm.user.$setPristine(true);
        });
    }
}