2014-07-26 7 views
4

각도 ui 라우터를 사용하여 앱을 만들고 있습니다.다시로드하지 않고 stateParams를 변경하십시오.

항상 매개 변수가 전달되는 특정 상태로 이동할 때보기를 다시로드하지 않고 URL을 변경하고 매개 변수를 업데이트 할 수 있어야합니다. 내 상태 설정에서

내가 사용하고 있습니다 : reloadOnSearch :

false이 작동하고 페이지를 다시로드하지 않고 URL의 매개 변수를 업데이트 할 날 수 있습니다.

그러나 $ stateParams에서 이러한 변경 사항을 통지 받아야 새 데이터로 작업 할 다양한 방법을 실행할 수 있습니다.

제안 사항?

감사합니다. James

+0

당신이 $ 범위를 할 수의의 상태 PARAMS를 반환하는 서비스를 구축했습니다. 작동하지 않습니다 $ stateParams – harishr

+2

에서 볼 $ .. – jameslouiz

+0

'$ routeUpdate' 이벤트에서 변경 기능을 수행 할 수 있습니까? reloadOnSearch가 false 일 때 URL 업데이트를 통해 브로드 캐스트되어야합니다. –

답변

3

이것은 약간의 작업을 수행했기 때문에 관련이 있습니다.

여기는 reloadOnSearch을 사용하지만 URL이 업데이트 될 때마다 기능이 중지되는 내 상태입니다. 콘솔

 .state('route2.list', { 
      url: "/list/:listId", 
      templateUrl: "route2.list.html", 
      reloadOnSearch: false, 
      controller: function($scope, $stateParams){ 
      console.log($stateParams); 
      $scope.listId = $stateParams.listId; 
      $scope.things = ["A", "Set", "Of", "Things"]; 
      //This will fire off every time you update the URL. 
      $scope.$on('$locationChangeSuccess', function(event) { 
       $scope.listId = 22; 
       console.log("Update"); 
       console.log($location.url()); 
       console.log($stateParams);  
       }); 
      } 
     } 

반환 : 당신이 가고 싶은 곳

Update 
/route2/list/7 (index):70 
Object {listId: "8"} (index):71 

$locationChangeStart$locationChangeSuccess 당신을 얻어야한다. 첫 번째로드에서도 위치 기능은 항상 꺼집니다.

감흥 ngRoute위한 각 문서에 따르면 SO 게시 this에서 촬영하고 $locationdocumentation

EDIT 2 , $routeParams이 아닌 변경의 성공 이후까지 갱신된다. ui-route에는 비슷한 제한이있을 가능성이 있습니다. 우리가 국가의 결의를 방해하기 때문에 $stateParams은 절대로 업데이트되지 않습니다. 당신이 할 수있는 일은 을 사용하여 처음에 $stateParams이 어떻게 전달되는지 에뮬레이트하는 것입니다. 그래서 같이

: 그건 좋은 일이 그래서

 .state('route2.list', { 
      url: "/list/:listId", 
      templateUrl: "route2.list.html", 
      reloadOnSearch: false, 
      controller: function($scope, $stateParams, $state, $urlMatcherFactory){ 
      console.log($stateParams.listId + " :: " + $location.url()); 
      $scope.listId = $stateParams.listId; 
      $scope.things = ["A", "Set", "Of", "Things"]; 
      $scope.$on('$locationChangeSuccess', function(event) { 
       $scope.listId = 22; 
       //For when you want to dynamically assign arguments for .compile 
       console.log($state.get($state.current).url); //returns /list/:listId 
       //plain text for the argument for clarity 
       var urlMatcher = $urlMatcherFactory.compile("/route2/list/:listId"); 
       //below returns Object {listId: "11" when url is "/list/11"} 
       console.log(urlMatcher.exec($location.url())); 
       var matched = urlMatcher.exec($location.url()); 
       //this line will be wrong unless we set $stateParams = matched 
       console.log("Update " + $stateParams.listId); 
       console.log($location.url()); 
       //properly updates scope. 
       $scope.listId = matched.listId; 
       }); 
      } 
     }) 

실제로 범위 업데이트. 기억해야 할 점은 당신이 모든 것을 정상적으로 해결하지만, reloadOnSearchfalse으로 설정하면 거의 모든 것을 스스로 처리해야한다는 것입니다.

+0

안녕하세요, 저를 도울 수있는 시간을내어 주셔서 감사합니다. 이것은 분명히 올바른 방향으로 나아가는 단계입니다. 이제 이벤트를 가지고 놀았습니다. 변경 후에 어떻게 새로운 파라미터를 얻을 수 있습니까? $ stateParams는 업데이트되지 않았습니다. – jameslouiz

+0

위치를 전달하면 위치 정보를 계속 사용할 수 있습니다. 경로 등의 기능을 사용하여 경로를 얻을 수 있습니다.'$ locationChangeStart'와'reloadOnSearch'를 사용하여 당신이 어떤 스코프 업데이트를 거의 멈추고 있음을 주목하십시오. '$ scope. $ apply()'도 이것과 함께 당신의 친구가 될 것입니다. 또한 최종 URL을 원하면'$ locationChangeSuccess'를 사용해야합니다. –

+0

예, 이해합니다.하지만 $ stateParams obj와 같은 객체를 반환해야합니다. 앵귤러는 아니지만 실제로 stateparams를 가져 오는 일종의 메소드가 있습니까? – jameslouiz

1

로마서 코드에 이어, 나는 현재 상태

/** 
* Get parameters from URL and return them as an object like in ui-router 
*/ 
eventaApp.service('getStateParams', ['$urlMatcherFactory', '$location', '$state', function( $urlMatcherFactory, $location, $state){ 
    return function() { 

    var urlMatcher = $urlMatcherFactory.compile($state.current.url); 
    return urlMatcher.exec($location.url()); 

    } 

}]); 
관련 문제