ionic - 登录页面不会重定向到主页面

时间:2016-10-15 06:06:50

标签: angularjs ionic-framework angular-ui-router

仅当登录页面用作后备时才会发生这种情况。当主页用作后备时,相同的登录页面正常工作。我对这个框架很新,任何帮助解决这个问题都会非常感激。如果您需要任何其他代码来解决此问题,请随时询问。提前谢谢!

这是我的配置,

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

    .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html'
})

.state('app.login', {
    url: '/login',
    views: {
        'menuContent': {
            templateUrl: 'templates/login.html',
            controller: 'AppCtrl'
        }
    },
    authenticate: false
})

.state('app.search', {
    url: '/search',
    views: {
        'menuContent': {
            templateUrl: 'templates/search.html',
            controller: 'mainCtrl'
        }
    }
})

.state('app.browse', {
        url: '/browse',
        views: {
            'menuContent': {
                templateUrl: 'templates/browse.html',
                controller: 'mainCtrl'
            }
        }
    })
    .state('app.playlists', {
        url: '/playlists',
        views: {
            'menuContent': {
                templateUrl: 'templates/playlists.html',
                controller: 'PlaylistsCtrl'
            }
        }
    })

.state('app.snapshot', {
    url: '/snapshot',
    views: {
        'menuContent': {
            templateUrl: 'templates/snapshot.html'
        }
    }
})

.state('app.callqueues', {
    url: '/callqueues',
    views: {
        'menuContent': {
            templateUrl: 'templates/callqueues.html'
        }
    }
})

.state('app.single', {
    url: '/playlists/:playlistId',
    views: {
        'menuContent': {
            templateUrl: 'templates/playlist.html',
            controller: 'PlaylistCtrl'
        }
    }
});
// if none of the above states are matched, use this as the fallback
//   $urlRouterProvider.otherwise('/app/search');
$urlRouterProvider.otherwise('/app/login');
});

这是我的loginCtrl(登录控制器),

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

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $location, $http, $log, $state) {

// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on('$ionicView.enter', function(e) {
//});

// Form data for the login modal
$scope.loginData = {};

// Create the login modal that we will use later
// $ionicModal.fromTemplateUrl('templates/login.html', {
//     scope: $scope
// }).then(function(modal) {
//     $scope.modal = modal;
// });

// Triggered in the login modal to close it
// $scope.closeLogin = function() {
//     $scope.modal.hide();
// };

// Open the login modal
// $scope.login = function() {
//     $scope.modal.show();
// };

$scope.chartConfig = {
    options: {
        chart: {
            type: 'bar'
        }
    },
    series: [{
        data: [10, 15, 12, 8, 7]
    }],
    title: {
        text: 'Hello'
    },

    loading: false
};

// Perform the login action when the user submits the login form
$scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);
    var username = $scope.loginData.name;
    var password = $scope.loginData.password;
    console.log("username" + $scope.loginData.name);
    if (username == "admin@as" && password == "admin") {
        console.log("if part");
        $location.path("#/app/search");
    } else {
        console.log("error");
        // $("#errorpwd").css({"display":"block"});
        // $scope.message = "Error";
        // $scope.messagecolor = "alert alert-danger";
    }
    // Simulate a login delay. Remove this and replace with your login
    // code if using a login system
    // $timeout(function() {
    //     $scope.closeLogin();
    // }, 1000);
};
})

最后这是我的登录html代码,

<ion-view view-title="Login" name="login-view">
<ion-header-bar class="bar bar-header bar-positive">
    <h1 class="title">Login</h1>
    <!--<button class="button button-clear button-primary icon-left ion-close-circled" ng-click="modal.hide()"></button>-->
</ion-header-bar>
<ion-content>
    <form ng-submit="doLogin()">
        <div class="list">
            <label class="item item-input">
      <span class="input-label">Username</span>
      <input type="text" ng-model="loginData.name">
    </label>
            <label class="item item-input">
      <span class="input-label">Password</span>
      <input type="password" ng-model="loginData.password">
    </label>
            <label class="item">
      <button nav-clear class="button button-block button-positive" type="submit">Log in</button>
    </label>
        </div>
    </form>
</ion-content>

1 个答案:

答案 0 :(得分:1)

您尚未将ui-router注入模块的依赖项

angular.module('starter.controllers', ['ui.router']);

也可以参考us.router的js

 <!-- Angular -->
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
  <!-- UI-Router -->
  <script src="//angular-ui.github.io/ui-router/release/angular-ui-router.js"></script>