2015-10-14 72 views
0

我有登錄頁面。AngularJs location.path不傳遞參數

的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> 

當我點擊登錄按鈕login()方法的工作在下面

LoginCtrl控制器

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http) { 
    $scope.login = function() { 
    $http({ 
     method: 'GET', 
     url: 'http://localhost:49431/api/values/GetUserInfo?username=' + $scope.data.username + '&password=' + $scope.data.password + '' 
    }).then(function successCallback(response) { 
     if (response.data.Status == true) { // Success 
     $location.path('/HomePage/').search({ id: '1' }); // Problem here 
     } else { // Fail 
     var alertPopup = $ionicPopup.alert({ 
      title: 'Login failed!', 
      template: 'Username or password is incorrect!' 
     }); 
     $scope.data.username = ""; 
     $scope.data.password = ""; 
     } 
    }, function errorCallback(response) { 
     alert("error"); 
    }); 
    } 
}) 

我用

$location.path('/HomePage/').search({ id: '1' }); 

代碼以id參數傳遞給HomePage.Html page.However既不位置路徑通參數也無法重定向page.Shortly位置路徑是行不通的。

HomePageCtrl控制器

.controller('HomePageCtrl', function ($scope, HomePageService, $state, $location, $cordovaCamera, $stateParams, $routeParams) { 
    alert($routeParams.id + $stateParams.id); 
} 

app.js

.config(function ($stateProvider, $urlRouterProvider) { 
    $stateProvider 
    .state('HomePage', { 
     url: '/HomePage', 
     templateUrl: 'templates/HomePage.html', 
     controller: 'HomePageCtrl' 
    }) 
    .state('login', { 
     url: '/login', 
     templateUrl: 'templates/Login.html', 
     controller: 'LoginCtrl' 
    }); 
    $urlRouterProvider.otherwise('/login'); 
}); 

問:

我怎麼能使用傳遞參數從LoginCtrl控制器HomePageCtrl控制器?

任何幫助將不勝感激。

謝謝。

+0

$ location.path()不會加載新頁面。 URL在瀏覽器中更新還是對$ location.path的調用看起來完全被忽略? –

回答

2

爲什麼你不使用狀態的$代替$位置

App.js:

.state('HomePage', { 
      url: '/HomePage/:id', 
      templateUrl: 'templates/HomePage.html', 
      controller: 'HomePageCtrl' 
    }) 

LoginCtrl:

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http) { 

      $scope.login = function() { 

      $http({ 
       method: 'GET', 
       url: 'http://localhost:49431/api/values/GetUserInfo?username=' + $scope.data.username + '&password=' + $scope.data.password + '' 
      }).then(function successCallback(response) { 

       if (response.data.Status == true) { // Success 

        $state.go('HomePage',{id:'1'}) 

       } 
       else { // Fail 
        var alertPopup = $ionicPopup.alert({ 
         title: 'Login failed!', 
         template: 'Username or password is incorrect!' 
        }); 

        $scope.data.username = ""; 
        $scope.data.password = ""; 
       } 

      }, function errorCallback(response) { 
       alert("error"); 
      }); 
    } 
    }) 

個HomeCtrl:

.controller('HomePageCtrl', function ($scope, HomePageService, $state, $location, $cordovaCamera, $stateParams, $routeParams) { 

    alert($stateParams.id); 
} 
2

我還沒有location.path嘗試過,但通常你需要在$ stateProvider的網址定義指定參數:

$stateProvider 
.state('HomePage', { 
    url: '/HomePage', 
    templateUrl: 'templates/HomePage.html', 
    controller: 'HomePageCtrl' 
}) 
.state('login', { 
    url: '/login/:userid/:password', 
    templateUrl: 'templates/Login.html', 
    controller: 'LoginCtrl' 
}) 

比你可以訪問這些參數在您的控制器中使用$stateParams.userid ...