简体   繁体   English

离子$ location.path()可在浏览器中使用,但不能在设备上使用

[英]Ionic $location.path() works in browser but not on devices

So, I have the following code that all works in the browser but for some reason keeps redirecting me back to login on the actual Android device. 因此,我有以下代码都可以在浏览器中使用,但是由于某种原因,我一直将我重定向回实际的Android设备上登录。

controllers.js controllers.js

App.controller('LoginController', ['$scope', 'RequestService', '$location', 'OpenFB', '$rootScope', function($scope, RequestService, $location, OpenFB, $rootScope) {

    $scope.provider = '';

    $scope.loginFacebook = function() {

        $scope.provider = 'facebook';

        OpenFB.login('email, user_friends').then(
            function () {
                OpenFB.get('/me').success(function (user) {
                    localStorage.setItem('fbuser', JSON.stringify(user));
                });

                localStorage.setItem('provider', 'facebook');

                RequestService.get($scope.baseUrl + 'user')
                    .success(function(data, status, headers, config){

                        $scope.user = data;

                        //Set our logged in var
                        localStorage.setItem('loggedIn', true);

                        // Check if the user has accepted EULA
                        if($scope.user.eula == 0) {
                            $location.path('/eula');
                        }
                        else
                        {
                            //TODO:Redirect to the users dashboard, when we have the routes.
                            console.log('EULA Accepted, Redirect somewhere else!');
                        }

                    });
            },
            function (error) {
                localStorage.removeItem('loggedIn');
            });
    };

}]);

I have debugged the above and everything is working as it should, once FB is logged in, it queries the database, sets the user up with the data scraped from Facebook and then checks that data to see if the EULA has been accepted. 我已经调试了上面的内容,并且一切正常,一旦FB登录,它将查询数据库,使用从Facebook抓取的数据设置用户,然后检查该数据以查看EULA是否已被接受。 If not then is should redirect to $location.path('/eula'); 如果不是,则应该重定向到$location.path('/eula');

routes 路线

.config(function($httpProvider, $stateProvider, $urlRouterProvider) {

    $httpProvider.interceptors.push('httpRequestInterceptor');

    $urlRouterProvider.otherwise('/');

    $stateProvider

    .state('login', {
        url: '/',
        templateUrl: 'templates/login.html',
        data: {
            requireLogin: false
        }
    })

    .state('eula', {
        url: '/eula',
        templateUrl: 'templates/eula.html',
        data: {
            requireLogin: true
        }
    })

        .state('accept', {
            url: '/accept',
            templateUrl: 'templates/accept.html',
            data: {
                requireLogin: true
            }
        })
});

as you are using ui-router (standard of ionic), you shall use states. 当您使用ui-router(离子标准)时,应使用状态。

Replace : $location.path('/eula'); 替换: $location.path('/eula'); by $state.go('eula'); 通过$state.go('eula'); (and correspondign injected dependency) (和对应注入的依赖)

Youre problem seems to have something to do with the execution. 您的问题似乎与执行有关。
OpenFB does not implement promises. OpenFB没有实现承诺。

Look at these lines: 看一下这些行:

OpenFB.get('/me').success(function (user) {
    localStorage.setItem('fbuser', JSON.stringify(user));
});

localStorage.setItem('provider', 'facebook');

It might take a while before OpenFB.get returns the result, but your code will execute the following instructions anyway, so you might find yourself in a situation where you're calling: OpenFB.get可能需要一段时间才能返回结果,但是您的代码仍将执行以下指令,因此您可能会遇到以下情况:

RequestService.get($scope.baseUrl + 'user')

before OpenFB.get('/me') has finished it's execution. OpenFB.get('/me')完成执行之前。

The best solution is to use ngOpenFB which implement promises: $q promises. 最好的解决方案是使用实现承诺的ngOpenFB$q promises。
There's a sample here . 有一个样品在这里

Your code should be implemented like this: 您的代码应这样实现:

ngFB.login({scope: 'email, user_friends'})
    .then(function(response){
        // response.authResponse.accessToken
        return ngFB.api({path: '/me'});
    })
    .then(function(user){
        localStorage.setItem('fbuser', JSON.stringify(user));
        localStorage.setItem('provider', 'facebook');
        RequestService.get($scope.baseUrl + 'user')
            .success(function(data, status, headers, config){
                $scope.user = data;

                //Set our logged in var
                localStorage.setItem('loggedIn', true);

                // Check if the user has accepted EULA
                if($scope.user.eula == 0) {
                    return false;
                } else
                {
                    return true;
                }
            })
            .error(function (data, status, headers, config) {
                return false;
            })
    })
    .then(function(eulaAccepted){
        if (eulaAccepted)
        {
            $state.go('eula');
        }
    })
    catch(function(reason){
        localStorage.removeItem('loggedIn');
    });

Each call returns a promise and they can be nested. 每个调用都返回一个Promise,并且可以嵌套它们。 The second .then( gets the result of the second promise (when it's been resolved) ngFB.api({path: '/me'}) . 第二个.then(得到第二个承诺的结果(当它被解决时) ngFB.api({path: '/me'})
Since that call returns a promise with the object user, you'll be able to read it in the function. 由于该调用会返回对象用户的promise,因此您可以在函数中读取它。

This code: 这段代码:

if($scope.user.eula == 0) {
     return false;
} else
{
    return true;
}

does not return a promise but it's ok with this type of syntax. 不会返回承诺,但是可以使用这种语法。

In the third .then( : 在第三个.then(

.then(function(eulaAccepted){
    if (eulaAccepted)
    {
        $state.go('eula');
    }
})

you'll be able to read the value of the previous statement (eula). 您将能够读取上一条语句(eula)的值。

Mind you, I haven't tested this code cause I don't have facebook, but the advantage of promises is to avoid indefinite levels of nested code 请注意,我没有测试此代码,因为我没有facebook,但是promises的优点是避免不确定级别的嵌套代码

Ok so solved the issue, in my login check in the .run() method of my app I had a miss placed 'event.preventDefault();' 好的,这样就解决了这个问题,在我的应用程序的.run()方法的登录检查中,我错过了一个'event.preventDefault();'的位置。 call, once this was moved the works on both the desktop browser and on my device. 调用后,将其移至桌面浏览器和我的设备上即可使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用location.path从registerBackButtonAction导航 - Unable to navigate from registerBackButtonAction using location.path 路径:“ / sdcard /”可在所有设备上使用吗? - Path : “/sdcard/” works in all devices? Android上具有angularjs的多设备混合应用程序:$ location.path()不起作用 - Multi-Device Hybrid Apps with angularjs on Android: $location.path() not working 离子图模板仅在浏览器中有效 - ionic maps template only works in browser ionic \\ cordova jsonp可在浏览器中使用,但不适用于ANDROID - ionic\cordova jsonp works in browser but not on ANDROID 离子白名单插件阻止了iPhone,但可以在浏览器中使用 - Ionic whitelist plugin blocks the iPhone but works in the browser Ionic View Google登录弹出窗口在Firebase 2中有效,但在Firebase 3中无效(在浏览器中有效) - Ionic View Google Login Popup works in Firebase 2, but not in Firebase 3 (works in browser) 位置侦听器在某些设备上无法正常工作,并且无法迁移到FusedLocationListener - Location listener have problems to works in some devices and problems to migrate to FusedLocationListener 位置来源设置意图仅适用于&lt;4.0的所有设备 - Location source settings intent works only with all devices < 4.0 在浏览器中播放HTML5视频 - 什么适用于大多数Android设备? - Playing HTML5 Videos in Browser — What Works for Most Android Devices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM