2013-12-08 116 views
18

我正在做類似於下面的應用程序,但我只是無法獲取routeChangeSuccess事件。

var myapp = angular.module('myapp', ["ui.router", "ngRoute"]); 

myapp.controller("home.RootController", 
    function($rootScope, $scope, $location, $route) { 
    $scope.menus = [{ 'name': 'Home ', 'link': '#/home'}, {'name': 'services', 'link': '#/services'} ] 

    $scope.$on('$routeChangeSuccess', function(event, current) { 

     alert('route changed'); 
    }); 

    } 
); 

myapp.config(
    function($stateProvider, $urlRouterProvider, $routeProvider) { 
    $urlRouterProvider.otherwise("/home"); 
    $stateProvider 
     .state('home', { 
     url: "/home", 
     //template: '<h1>Home Screen</h1>' 
     templateUrl: "/Client/Views/Home/Home.htm" 
     }) 
     .state('services', { 
     url: "/services", 
     //template: '<h1>Service screen</h1>' 
     templateUrl: "/Client/Views/Home/service.htm" 

     }); 
    }); 

一個非常簡單的HTML如下也會失敗

<body ng-controller="home.RootController"> 

    <ul class="nav"> 
     <li ng-repeat="menu in menus" "=""> 
     <a href="{{menu.link}}">{{menu.name}}</a> 
     </li> 
    </ul> 
    <div ui-view> No data yet!</div> 
    </body> 

但是當我的鏈接我看到視圖得到更新點擊,但從未觸發$ routeChangeSucces事件。

有什麼我失蹤?

另一個問題我曾經是有一個事件,我可以掛鉤知道視圖已準備好,所以我可以開始一些額外的處理,如document.ready()。

plnlr但不是完全的工作...

問候 基蘭

+0

'ui.router'不適用於'ngRoute'。請使用其中一個或另一個。 –

回答

41

請查看這個wiki:State Change Events。摘錄:

  • $ stateChangeSuccess - 狀態轉換完成後觸發。

    $範圍。在$( '$ stateChangeSuccess',
    功能(事件,使用toState,toParams,fromState,fromParams){...})

因此而不是$routeChangeSuccess使用$stateChangeSuccess

要獲取有關所有可用事件的更多詳細信息,請查看wiki Events在這裏你可以發現適合你,可能是事件$viewContentLoaded ...現在

+0

謝謝! $ viewContentLoaded是我正在尋找的。順便說一句,我無法獲得事件$ viewContentLoading。不知道爲什麼。但$ viewContentLoaded可以正常工作 – Kiran

+0

類似地,請查看'$ stateChangeStart'。 – weltschmerz

0

stateChange活動已被取消,刪除,使用transitions代替。

$transitions.onSuccess({}, function() { 
    console.log("state changed"); 
}); 
相關問題