2013-08-16 1 views
2

라우팅과 나는 자바 스크립트에서 몇 가지 다른 URL에 대한 경로 아주 간단한 각도 프로젝트가 :사용자 정의 상태를 입력하고 국가 종료 코드

function RootController($scope) {}; 

function PageOneController($scope) {}; 

angular.module('mymodule', []).config(
    ['$routeProvider', function($routeProvider) { 
     $routeProvider.when('/', { 
      templateUrl: "templates/root.html", 
      controller: RootController 
     }).when('/page1/', { 
      templateUrl: "templates/page1.html", 
      controller: PageOneController 
     }).otherwise({redirectTo: '/'}); 
    }] 
); 

다 큰 지금까지 작동을하지만, 내가 필요합니까 길을 입력하고 나가면 일부 자바 스크립트 기능을 실행할 수 있습니다. 예 :

$routeProvider.when('/', { 
    templateUrl: "templates/root.html", 
    controller: RootController, 
    onenter: function() { console.log("Enter!"); }, 
    onexit: function() { console.log("Exit!"); } 
}); 

Angular? 상태/경로를 입력 할 때 이벤트 리스너를 바인딩해야하며 종료 할 때 해당 이벤트를 삭제하고 해체해야합니다.

답변

5

$route service에는 두 가지 이벤트 인 $routeChangeStart$routeChangeSuccess이 있습니다. 이는 약간의 도움이 될 수 있습니다.

퇴장 전에 $routeChangeStart을 입력하고 입력시 $routeChangeSuccess을 사용할 수 있습니다.

모든 컨트롤러에서 $scope.$on을 수행하면 이러한 이벤트를 볼 수 있습니다.

관련 문제