【问题标题】:$state.go is not working when project is run with ionic serve --lab command使用 ionic serve --lab 命令运行项目时,$state.go 不起作用
【发布时间】:2017-03-03 02:40:28
【问题描述】:

您好,我正在使用 ionic 框架,我想重定向到另一个页面

成功登录后。当我使用命令运行项目时:

ionic serve 然后 $state.go 正常工作,但是当我使用

运行项目时

ionic serve --lab 不工作

 .controller('AppCtrl', function($scope,$http, $ionicModal,$location,$state, $timeout) {

    // Form data for the login modal
    $scope.loginData = {};

    // Create the login modal that we will use later
    $ionicModal.fromTemplateUrl('templates/login.html', {
        scope: $scope
        }).then(function(modal) {
        $scope.modal = modal;
    });

    // Triggered in the login modal to close it
    $scope.closeLogin = function() {
        $scope.modal.hide();
    };

    // Open the login modal
    $scope.login = function() {
        $scope.modal.show();
    };

    // Perform the login action when the user submits the login form
    $scope.doLogin = function() {

        alert($scope.loginData.username);
        alert($scope.loginData.password);

        $http({
            method: 'POST',
            url: 'http://localhost/home_owner12/admin/api/login',
            data: {username:$scope.loginData.username,password:$scope.loginData.password},
            headers : {
            'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'}
        })
        .then(function successCallback(response) 
        {

            if(response.data.length > 0)
            {

                 $state.go('app.search');
                 console.log('the state is '+$state.current);

            }
            else 
            {
                alert("Invalid email or pasword");
                ///$location.path('/search');

            }
        }, function errorCallback(response) 
        {
            alert("Invalid email pasword");
        });
    };
    })

这里是状​​态:

config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

    .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  })

 .state('app.search', {
    url: '/search',
    views: {
      'menuContent': {
        templateUrl: 'templates/search.html',
        controller: 'AppCtrl'
      }
    }
  });

和模块

angular.module('starter', ['ionic', 'starter.controllers', 'ui.router'])

【问题讨论】:

  • console.log('the state is '+$state.current); 被执行了吗?
  • @lin 当我使用结果是:-状态是 [object Object]

标签: angularjs ionic-framework mean-stack


【解决方案1】:

app.search 页面的控制器就地转到 AppCtrl。

因为 AppCtrl 第二次运行,它进入了一个无限循环。

用那个替换代码

config(function($stateProvider, $urlRouterProvider) {
          $stateProvider

          .state('app', {
            url: '/app',
            abstract: true,
            templateUrl: 'templates/menu.html',
            controller: 'AppCtrl'
          })

         .state('app.search', {
            url: '/search',
            views: {
              'menuContent': {
                templateUrl: 'templates/search.html',
                controller: 'SearchCtrl'
              }
            }
          });

 .controller('SearchCtrl', function($scope,$http,    $ionicModal,$location,$state, $timeout) {
............
.........
.......
 })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 2015-08-12
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多