简体   繁体   English

Ionic / AngularJS重定向到另一个页面

[英]Ionic/AngularJS redirect to another page

I have login page . 我有登录页面。 When username and password is correct i want to redirect from login page to HomePage.html page. 当用户名和密码正确时,我想从登录页面重定向到HomePage.html页面。

Login.html 的login.html

<ion-view view-title="Login" name="login-view">
  <ion-content class="padding">
      <div class="list list-inset">
          <label class="item item-input">
              <input type="text" placeholder="Username" ng-model="data.username">
          </label>
          <label class="item item-input">
              <input type="password" placeholder="Password" ng-model="data.password">
          </label>
      </div>
      <button class="button button-block button-calm" ng-click="login()">Login</button>
  </ion-content>
</ion-view>

HomePage.html HomePage.html

<label id="test">test</label>

controller.js controller.js

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state) {
    $scope.data = {};

    $scope.login = function () {
        LoginService.loginUser($scope.data.username, $scope.data.password).success(function (data) {

            $state.go('/HomePage'); // This is not working for me

        }).error(function (data) {
            var alertPopup = $ionicPopup.alert({
                title: 'Login failed!',
                template: 'Please check your credentials!'
            });
        });
    }
})

Question: 题:

When i try below code 当我尝试下面的代码

$state.go('/HomePage');

this is not working for me. 这不适合我。

I get below exception as below 我得到以下异常如下

Error: Could not resolve '/HomePage' from state 'login'

How can i reach homepage.html page from login page. 如何从登录页面访问homepage.html页面。

Any help will be appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

use this : 用这个 :

$location.path('/HomePage');

Full code: 完整代码:

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup,$location) {
    $scope.data = {};

    $scope.login = function () {
        LoginService.loginUser($scope.data.username, $scope.data.password).success(function (data) {

            $location.path('/HomePage'); // working

        }).error(function (data) {
            var alertPopup = $ionicPopup.alert({
                title: 'Login failed!',
                template: 'Please check your credentials!'
            });
        });
    }
})

use this : 用这个 :

$state.go('app.HomePage');    //This is worked for me.

My Code: 我的代码:

 $scope.Login = function(form){  
   if(form.$valid) {   

          $http({
          method  : 'POST',
          url     : link,
          data    : {username : $scope.data.username, password : $scope.data.password,action: 'login'}, //forms user object
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
         })
          .success(function(data) {                
             $state.go('app.listings');                             
          });           
    }       
 };

Use state name instead of a path. 使用州名而不是路径。 For example: 例如:

$stateProvider
    .state('auth', { 
        url: '/auth', 
        views: { 
            'nav_view_start': { 
                templateUrl: 'templates/auth/auth.html', 
                controller: 'AuthCtrl'
            }
        }
    })
    .state('main', { 
        url: '/main', 
        abstract: true, 
        cache: false, 
        views: { 
            'nav_view_start': { 
                templateUrl: 'templates/main/main.html', 
                controller: 'MainCtrl' 
            }
        } 
    }); 

Use main instead of /main . 使用main而不是/main

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM