最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IonicAngularJS redirect to another page - Stack Overflow

programmeradmin4浏览0评论

I have login page . When username and password is correct i want to redirect from login page to HomePage.html page.

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

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

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.

Any help will be appreciated.

Thanks.

I have login page . When username and password is correct i want to redirect from login page to HomePage.html page.

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

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

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.

Any help will be appreciated.

Thanks.

Share Improve this question edited May 4, 2017 at 20:26 Upalr 2,1482 gold badges25 silver badges33 bronze badges asked Oct 12, 2015 at 11:23 SonerSoner 1,2823 gold badges16 silver badges40 bronze badges 2
  • 2 post you app.js here – Hardik Gondalia Commented Oct 12, 2015 at 11:27
  • 1 i also forgot to add new path to app.js thanks for answer – Soner Commented Oct 12, 2015 at 11:32
Add a comment  | 

3 Answers 3

Reset to default 11

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.

发布评论

评论列表(0)

  1. 暂无评论