Programming

AngularJS에서 경로 변경을 감시하는 방법?

procodes 2020. 5. 5. 21:00
반응형

AngularJS에서 경로 변경을 감시하는 방법?


경로 변경시 이벤트를 어떻게보고 트리거합니까?


참고 : 이것은 기존 버전의 AngularJS에 대한 정답입니다. 업데이트 된 버전에 대해서는 이 질문참조하십시오 .

$scope.$on('$routeChangeStart', function($event, next, current) { 
   // ... you could trigger something here ...
 });

다음과 같은 이벤트도 사용할 수 있습니다 (콜백 함수에는 다른 인수가 사용됨).

  • $ routeChangeSuccess
  • $ routeChangeError
  • $ routeUpdate- reloadOnSearch 특성이 false로 설정된 경우

$ route 문서를 참조하십시오 .

문서화되지 않은 다른 두 가지 이벤트가 있습니다.

  • $ locationChangeStart
  • $ locationChangeSuccess

$ locationChangeSuccess와 $ locationChangeStart의 차이점무엇입니까?를 참조하십시오 .


시계를 특정 컨트롤러에 배치하지 않으려는 경우 Angular 앱에서 전체 응용 프로그램에 대한 시계를 추가 할 수 있습니다 run()

var myApp = angular.module('myApp', []);

myApp.run(function($rootScope) {
    $rootScope.$on("$locationChangeStart", function(event, next, current) { 
        // handle route changes     
    });
});

$rootScope.$on( "$routeChangeStart", function(event, next, current) {
  //..do something
  //event.stopPropagation();  //if you don't want event to bubble up 
});

$rootScope.$on( "$routeChangeStart", function(event, next, current) {
  //if you want to interrupt going to another location.
  event.preventDefault();  });

참고 URL : https://stackoverflow.com/questions/14765719/how-to-watch-for-a-route-change-in-angularjs

반응형