使用ionic serve --lab命令运行项目时,$ state.go无效

时间:2017-03-02 13:01:05

标签: angularjs ionic-framework mean-stack

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

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

离子服务然后$ 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'])

1 个答案:

答案 0 :(得分:0)

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) {
............
.........
.......
 })