$ location.path第一次无法使用Ionic / Firebase [英] $location.path not working first time with Ionic/Firebase

查看:99
本文介绍了$ location.path第一次无法使用Ionic / Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个移动应用程序,使用Ionic.io(版本1)和Firebase(最新版本,我相信3)用于数据库。我正在尝试设置注册页面,以便在注册成功后,网站会将您重定向到另一个页面以填写您的个​​人资料。但是,当我点击注册按钮时,虽然用户已成功创建,但$ location.path('...')不执行任何操作(尽管重定向确实显示在控制台中,请参见下文)。在第二次点击时,它按预期重定向页面。

I am building my first mobile app, using Ionic.io (version 1) and Firebase (latest version, 3 I believe) for the database. I am trying to set up the sign-up page such that, upon successful registration, the site redirects you to another page to fill in your profile. However, when I hit the signup button, although the user is successfully created, $location.path('...') does nothing (although 'Redirecting' does display in the console, see below). Upon a second click, it redirects the page as expected.

我尝试了几件事,包括引入$ scope。$ apply,因为我认为你可以'在服务/工厂内使用$ scope。我也尝试在路径的前面添加一个#。使用$ location.url()也无济于事。我设法通过引入超时来解决问题,但这感觉有点hacky - 为什么会出现这个问题并且有更好的解决方案吗?

I tried several things, including introducing $scope.$apply, which went horribly wrong as I believe you can't use $scope inside a service/factory. I also tried appending a # at the front of the path. Using $location.url() doesn't help either. I managed to solve the problem by introducing a timeout, but this feels a bit hacky - why is this problem occurring and are there any better solutions?

谢谢!

服务:

signupEmail: function(newEmail, newPassword, newType) {
            var user;
                    firebase.auth().createUserWithEmailAndPassword(newEmail, newPassword)
                        //{email: newEmail,
                        //password: newPassword,}
                    .then(function(success) {
                        console.log("Successfully signed up!");
                        console.log(success);
              user = firebase.auth().currentUser;
              console.log("User....")
              console.log(user)
            }).then(function() {
              firebase.database().ref('users/' + user.uid).set({
                type: "..."
              })    
              console.log("Redirecting...");
              $timeout(function(){ 
                $location.path("/profile"); 
              },0);
            })
                    .catch(function(error) {
                        console.log(error.code)
                        console.log(error.message)
                    })
                },

控制器:

function SignupCtrl($scope, AuthService) {
    $scope.data = {};

    $scope.createUser = function() {
        var newEmail = $scope.data.email;
        var newPassword = $scope.data.password;
        var newType = $scope.data.type;
        AuthService.signupEmail(newEmail, newPassword, newType)
    };
}


推荐答案

AuthService.signupEmail(newEmail, newPassword, newType)
 .then(function(_user) {
    $state.go('profile', _user )
 }, function(_error) {
    console.log(_error)
 });

让服务返回承诺...

have the service return the promise...

signupEmail: function(newEmail, newPassword, newType) {
     var user;

     return firebase.auth().createUserWithEmailAndPassword(newEmail, newPassword)
          .then(function(success) {
              console.log("Successfully signed up!");
              console.log(success);
              user = firebase.auth().currentUser
              return user; 
           }).then(function(_user) {
               firebase.database().ref('users/' + user.uid).set({})
               return user;
           })
    },

这篇关于$ location.path第一次无法使用Ionic / Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆