Angularjs ng-click点击重定向

时间:2016-10-10 17:32:48

标签: javascript html angularjs firebase angularjs-ng-click

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

应将其重定向到$location.path('/login.signin');,但只有在我再次单击按钮时才会重定向到<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> 。奇怪。这是从哪里来的?

这是我的HTML:

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;
        });
    }

}]);

这是我的控制器:

eval

修改:我无法弄清楚如何将此question的答案与我的问题联系起来。

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

1 个答案:

答案 0 :(得分:1)

从那个问题 - 看起来重大的变化是使用$state.go('profile', _user ),它是ui-router的一部分,而不是$location.path,但由于这是第三方库 - 它可能是在角度消化之外被称为。假设您正在使用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的调用,并将承诺返回给控制器中的调用。

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
          });
      }
   }
}]);

并在你的控制器内:

$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;
    });
}