我有一个密码更新表格。
HTML:

<div class="profile-fields">

                <p>Please provide your profile details</p>

                <div class="field">
                    <label for="username">User Name</label>
                    <input type="text" name="username" value="" placeholder="UserName" ng-model="profile.name" ng-disabled="true" />
                </div>

                <div class="field">
                    <label for="password">Password:</label>
                    <input type="password" name="password"  placeholder="Password" ng-model="profile.password" value="" validator="required" required-error-message="Password is required" valid-method="watch" />
                </div>
                <div class="field">
                    <label for="password">Retype Password:</label>
                    <input type="password" name="password1" value="" placeholder="Password" ng-model="password1" validator="required" required-error-message="Password is required" valid-method="watch" />
                </div>
            </div>


使用ng-model="profile.password"从第一个密码框中发送新密码。

控制器:

myApp.controller('profileController', ['$scope', 'userResolved', 'userServices', '$location', function ($scope, userResolved, userServices, $location) {

    $scope.oldPassword = userResolved.data.password;
    $scope.profile = userResolved.data;
$scope.cancelProfile = function () {


        $location.path("/dashboard");


    }
    $scope.updateProfile = function () {

        if ($scope.profileForm.$valid && $scope.profile.password == $scope.password1) {
            if ($scope.profile.password != $scope.oldPassword) {
                userServices.saveProfile($scope.profile).then(function (result) {
                    if (!result.error) {
                        alert("Profile updated!!! Please login again with new password.");
                        $location.path("/logout");
                    }
                })
                .catch(function (data) {
                    $scope.error = data.data;
                });
            }
            else {
                $scope.error = "Password cannot be same as before!!"

            }

        }
        else {
            $scope.error = "Passwords mismatch!!"
        }
    };
}]);


问题在于,在页面加载时,模型在第一个密码文本框中显示当前密码。用户必须从第一个文本框中手动清除当前密码,然后输入新密码。我不能只清除profile.password,因为我正在使用它进行验证。无论如何,是否可以通过不操纵ng-model的方式清除页面加载中元素内的值?

文本框不应在页面加载时显示当前密码。我该如何实现?

最佳答案

创建一个新的变量,并为该变量分配密码,然后清除model变量。

var myPassword = profile.password;

profile.password = "";

09-17 16:34
查看更多