2014-07-10 3 views
0

ng-view을 사용하여 각도로 양식 제출시 부분 템플릿으로 라우팅하려합니다. URL과 함께 매개 변수 ?username=Joe을 전달하고 있는데, 이는 $routeParams을 사용하여 새 부분에서 검색하기를 바랍니다.요청 매개 변수로 각도 라우팅 실패

내가 어떤 매개 변수를 전달하지 않는 경우 라우팅 작동

: $location.path('/submit'); 하지만 매개 변수를 전달하면 폼 버튼이 작동하지 않습니다 $location.path('/submit?userName=joe');

각도 코드 :

index.html을 :

<body> 
<div ng-view></div> 
</body> 

loginPartial.html :

,210
<form ng-submit="submit()"> 
    <button type="submit">Submit</button> 
</form> 

messagePartial.html :

<p>Hello {{userName}}<p> 

각도 코드 :

angular.module('app',['ngRoute']) 
.config(['$routeProvider', 
function($routeProvider){ 
    $routeProvider 
     .when('/',{ 
      templateUrl:'loginPartial.html', 
      controller:'LoginController' 
     }) 
     .when('/submit',{ 
      templateUrl:'messagePartial.html', 
      controller:'MessageController' 
     }) 
     .otherwise({ 
      redirectTo:'/' 
     }); 

}]) 
.controller('LoginController',['$scope','$location', 
function($scope,$location){ 
    $scope.submit=function(){ 
    $location.path('/submit');//routing works with no parameters passed 
    //$location.path('/submit?userName=joe');//routing fails if parameters passed 
    }; 
}]) 
.controller('MessageController',['$scope','$routeParams', 
function($scope,$routeParams){ 
    $scope.userName=$routeParams.userName; 
}]); 
당신은 $routeParams 작업 시도가 아니다는 것을 것을을 발견하고 또한 $location.path('/submit').search({userName:'Joe'})

를 사용할 필요가

답변

0

주입 $route 다음 컨트롤러에서 사용 : var _params = $route.current.params