gpt4 book ai didi

javascript - Angularjs ng-click 在几秒钟内点击重定向

转载 作者:行者123 更新时间:2023-12-03 05:55:11 24 4
gpt4 key购买 nike

为什么我的 ng-click 仅在第二次点击时才会重定向到新位置?

它应该被重定向到 $location.path('/login.signin'); 但是只有当我再次单击该按钮时它才会这样做。奇怪的。这是从哪里来的?

这是我的 HTML:

<div class="box-forgot" ng-controller="ResetPass">
<form class="form-forgot" name="resetpassword">
<fieldset>
<legend>
Reset Password:
</legend>
<p>
Enter your new password below to reset the old password:
</p>
<div class="form-group">
<span class="input-icon">
<input type="password" class="form-control" id="password" ng-model="user.password" ng-minlength="8" name="password" placeholder="Password" required="">
<i class="fa fa-lock"></i> </span>
<p class="help-block" ng-show="registration.password.$dirty && registration.password.$error.minlength">Too short! </p>

</div>
<div class="form-actions">
<button class="btn btn-primary pull-right" ng-disabled="resetpassword.$invalid" ng-click="resetMe()">
Reset <i class="fa fa-arrow-circle-right"></i>
</button>
</div>
</fieldset>
</form>
</div>

这是我的 Controller :

app.controller("ResetPass", ["$scope","firebase", "$location",
function ($scope,firebase, $location) {

$scope.resetMe = function () {
var newPassword = $scope.user.password;
var actionCode = $location.search().oobCode;
var mode = $location.search().mode;
firebase.auth().confirmPasswordReset(actionCode, newPassword)
.then(function (resp) {
console.log("reset pass, done");
$location.path('/login.signin');
}).catch(function (error) {
$scope.errMsg = true;
$scope.errorMessage = error.message;
});
}

}]);

编辑:我不知道如何将答案与此 question 联系起来我的问题。

编辑 2:对于 ui 路由器状态,这里是 link .

最佳答案

从这个问题来看,重大变化似乎是使用 $state.go('profile', _user ),它是 ui-router 的一部分,而不是 $location。路径,但由于这是一个第三方库 - 它可能在 Angular 的摘要之外被调用。假设您使用的是 ui-router,我会尝试以下操作:将您的调用替换为 $state.go('login.signin')

$scope.resetMe = function () {
var newPassword = $scope.user.password;
var actionCode = $location.search().oobCode;
var mode = $location.search().mode;
firebase.auth().confirmPasswordReset(actionCode, newPassword)
.then(function (resp) {
console.log("reset pass, done");
$location.path('/signin').replace(); // Add this
$state.go('login.signin'); // and this
}).catch(function (error) {
$scope.errMsg = true;
$scope.errorMessage = error.message;
});
}

该文章中的另一个重大变化是,他们将对 firebase 的调用封装在服务中,并将 promise 返回到 Controller 中的调用。

app.factory("AuthenticationService", ["firebase", function (firebase) {
// Inside your service
return {
resetPassword: function(actionCode, newPassword) {
// return this next line
return firebase.auth().confirmPasswordReset(actionCode, newPassword)
.then(function (resp) {
console.log("reset pass, done");
return resp; // return the response
});
}
}
}]);

在你的 Controller 内部:

$scope.resetMe = function () {
var newPassword = $scope.user.password;
var actionCode = $location.search().oobCode;
var mode = $location.search().mode;
AuthenticationService.resetPassword(actionCode, newPassword)
.then(function (resp) {
$scope.$apply(function() {
$location.path('/signin').replace();
$state.go('login.signin');
});
}).catch(function (error) {
$scope.errMsg = true;
$scope.errorMessage = error.message;
});
}

关于javascript - Angularjs ng-click 在几秒钟内点击重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39963722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com