【问题标题】:Ionic/AngularJS redirect to another pageIonic/AngularJS 重定向到另一个页面
【发布时间】:2017-05-05 04:26:25
【问题描述】:

我有登录页面。当用户名和密码正确时,我想从登录页面重定向到 HomePage.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

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

问题:

当我尝试下面的代码时

$state.go('/HomePage');

这对我不起作用。

我得到以下异常

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

如何从登录页面访问 homepage.html 页面。

我们将不胜感激。

谢谢。

【问题讨论】:

  • 在这里发布 app.js
  • 我也忘记添加新路径到 app.js 感谢回答

标签: javascript html angularjs ionic-framework


【解决方案1】:

使用这个:

$location.path('/HomePage');

完整代码:

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

【讨论】:

  • 这是使用 Angular 的路由器,现在您正在以两种不同的方式处理状态。
【解决方案2】:

使用这个:

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

我的代码:

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

【讨论】:

    【解决方案3】:

    使用状态名称而不是路径。例如:

    $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

    【讨论】:

    • $stateProvider.state(''auth', { url : '/auth', 视图: { 'nav_view_start' : { templateUrl : 'templates/auth/auth.html', 控制器: 'AuthCtrl ' } } }).state('main',{ url:'/main',摘要:true,缓存:false,视图:{'nav_view_start':{ templateUrl:'templates/main/main.html',控制器: 'MainCtrl' }, } });

      使用“main”而不是“/main”
    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 2015-03-12
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多