【问题标题】:Ionic $urlRouterProvider.otherwise() not working离子 $urlRouterProvider.otherwise() 不工作
【发布时间】:2017-03-22 01:51:31
【问题描述】:

我尝试了以下教程以及查看其他 stackoverflow 问题,但我不知道我做错了什么。

我用 ionic creator 构建了这个,现在我试图让用户登录到谷歌,然后是到我的仪表板的完整路径。

抱歉,这对我来说是全新的。如果你能解释我做错了什么以及为什么会这样,那么我可以学习。谢谢!

我的控制器.js:

angular.module('app.controllers', [])

.controller('loginCtrl', ['$scope', '$stateParams', // The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams, $urlRouterProvider) {

    $scope.googlelogin = function(){
        //alert('yup');
        var provider = new firebase.auth.GoogleAuthProvider();
        provider.addScope('https://www.googleapis.com/auth/plus.login');
        firebase.auth().signInWithPopup(provider).then(function(results){
            // This gives you a Google Access Token. You can use it to access the Google API.
            var token = results.credential.accessToken;
            // The signed-in user info.
            var user = results.user;
            console.log(user);

            $urlRouterProvider.otherwise('/dashboard');
            //window.location.href = "dashboard.html";
        });
    }
}])

我的 login.html:

<ion-view title="Login" hide-nav-bar="true" id="page4">
    <ion-content padding="true" class="manual-ios-statusbar-padding">
        <form id="login-form1" class="list">
            <div class="spacer" style="width: 300px; height: 40px;"></div>
            <div>
                <img src="img/7TKZQZBSSg62UJQB4zyu_logo.png" width="30%" height="auto" style="display: block; margin-left: auto; margin-right: auto;">
           </div>
           <div class="spacer" style="width: 300px; height: 100px;"></div>
           <a id="login-button1" class="button button-stable  button-block" ng-click="googlelogin()">Google Login</a>
           <button id="login-button3" class="button button-stable  button-block">Facebook Login</button>
       </form>
   </ion-content>
</ion-view>

Routes.js:

angular.module('app.routes', [])

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

    // Ionic uses AngularUI Router which uses the concept of states
    // Learn more here: https://github.com/angular-ui/ui-router
    // Set up the various states which the app can be in.
    // Each state's controller can be found in controllers.js
    $stateProvider



    .state('menu.dashboard', {
        url: '/dashboard',
        views: {
            'side-menu21': {
                templateUrl: 'templates/dashboard.html',
                controller: 'dashboardCtrl'
             }
        }
    })

    .state('login', {
        url: '/login',
        templateUrl: 'templates/login.html',
        controller: 'loginCtrl'
    })

$urlRouterProvider.otherwise('/side-menu21/dashboard')
});

【问题讨论】:

    标签: android angularjs ionic-framework firebase-authentication google-login


    【解决方案1】:

    你的js应该是这样的

    angular.module('app.controllers', [])
    
        .controller('loginCtrl', ['$scope', '$stateParams', '$state','$location',
        function ($scope, $stateParams,$state,$location, $urlRouterProvider) {
    
            $scope.googlelogin = function(){
    
                var provider = new firebase.auth.GoogleAuthProvider();
                provider.addScope('https://www.googleapis.com/auth/plus.login');
                firebase.auth().signInWithPopup(provider).then(function(results){
    
                    var token = results.credential.accessToken;
    
                    var user = results.user;
                    console.log(user);
    
                   $state.go('menu.dashboard'); 
    
    //or you may use location like below
    //$location.path('/dashboard');
                    });
                }
            }])
    

    【讨论】:

    • 当我尝试时,我得到一个错误:未捕获的错误:无法从状态“登录”(...)解析“/dashboard”
    • 实际上是它的 $state.go('menu.dashboard')。那解决了它。谢谢!
    【解决方案2】:

    你应该使用$state.go('menu.dashboard'); 而不是$urlRouterProvider.otherwise('/dashboard'); Ionic 使用 angular ui 路由器,您可以在此处阅读更多信息:https://github.com/angular-ui/ui-router

    【讨论】:

    • Error: Uncaught TypeError: $scope.go is not a function(...)
    • 啊,抱歉是 $state.go 而不是 $scope.go 我在这里睡觉:记得注入 $state
    • 检查下面的解决方案,我收到一个错误:未捕获的错误:无法从状态“登录”(…)解析“/dashboard”
    【解决方案3】:

    您可以使用如下 $location 服务,

    angular.module('app.controllers', [])
    .controller('loginCtrl', ['$scope', '$stateParams', '$state','$location',
        function ($scope, $stateParams,$state,$location, $urlRouterProvider) {
            $scope.googlelogin = function(){
                var provider = new firebase.auth.GoogleAuthProvider();
                provider.addScope('https://www.googleapis.com/auth/plus.login');
                firebase.auth().signInWithPopup(provider).then(function(results){
                    var token = results.credential.accessToken;
                    var user = results.user;
                    console.log(user);
                    $location.path('dashboard'); 
                });
            }
        }])
    

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      相关资源
      最近更新 更多