Ionic / AngularJS redirects to another page

I have a login page. When the username and password are correct, I want to redirect from the login page to the 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 to make the code below

$state.go('/HomePage');

this does not work for me.

I get below exceptions as below

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

How can I go to the homepage.html page from the login page.

Any help would be appreciated.

Thanks.

+4
source share
3 answers

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!'
            });
        });
    }
})
+10
source

use this:

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

My code is:

 $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');                             
          });           
    }       
 };
+5
source

. :

$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' 
            }
        } 
    }); 

main /main.

+3

Source: https://habr.com/ru/post/1611277/


All Articles