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

标签 javascript html angularjs firebase angularjs-ng-click

为什么我的 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/

相关文章:

javascript - cheerio 使用 href 和 span 解析 h2

javascript - stuts2 Action 和 jquery ajax

css - 在弧形框内创建居中弧形框

javascript - Cordova 加载工厂数据

javascript - 使用ng-repeat的数据

javascript - 一切都是表达

javascript - 我们如何卡住html表格中的第一列和标题(有多行)

javascript - 通过 JQuery 关注 HTML 元素的下一个 tabindex onEnter keypress

php - 如何为横幅标志设置超链接。请帮忙

javascript - 页面刷新时清除服务中的数据