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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33079878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:36:45  来源:igfitidea点击:

Ionic/AngularJS redirect to another page

javascripthtmlangularjsionic-framework

提问by Soner

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

登录.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

主页.html

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

controller.js

控制器.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.

谢谢。

回答by Upal Roy

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

回答by Sathibabu P

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

回答by Ranjith

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 maininstead of /main.

使用main代替/main