2014-02-06 48 views
19

第一次測試UI路由器,但現在測試事件,我似乎無法理解如何觸發$ viewContentLoaded或加載。雖然,我有stageChangeSuccess,等工作!我只是推動一切到http://punkbit.com/space_competition/,但也在這裏添加了一些代碼。我期望在將新視圖加載到ui-view時觸發事件。但我想我在這裏錯過了一些東西!如何觸發UI-Router視圖加載事件?

<div class="pure-g"> 
     <div class="pure-u-1" ui-view> 
     </div> 
    </div> 


    <!-- s: template partials --> 
    <script type="text/ng-template" id="menu.html"> 
    <div class="pure-menu pure-menu-open pure-menu-horizontal"> 
     <ul> 
      <li><a href="#/home">home</a></li> 
      <li class="pure-menu-selected"><a href="#/like_gate">like_gate</a></li> 
      <li><a href="#/terms_and_conditions">terms_and_conditions</a></li> 
      <li><a href="#/enter_competition">enter_competition</a></li> 
     </ul> 
    </div> 
    </script> 
    <script type="text/ng-template" id="home.html"> 
    <p>home.html template! fbLike is {{fbLike}}</p> 
    </script> 
    <script type="text/ng-template" id="enter_competition.html"> 
    <p>enter_competition.html template!</p> 
    </script> 
    <script type="text/ng-template" id="like_gate.html"> 
    <p>like.html template!</p> 
    </script> 
    <script type="text/ng-template" id="terms_and_conditions.html"> 
    <p>terms_and_conditions.html template!</p> 
    </script> 
    <!-- e: template partials --> 

main.js

angular.module("space_competition", ['ui.router']) 

.config(function($stateProvider, $urlRouterProvider, $locationProvider){ 

    $stateProvider 
     .state('home', { 
      url: '/home', 
      templateUrl: 'home.html', 
      controller: 'homeCtrl', 
      resolve: { 
       fb_like: 'fbLike' 
      } 
     }) 
     .state('enter_competition', { 
      url: '/enter_competition', 
      templateUrl: 'enter_competition.html', 
      controller: 'enterCompetitionCtrl', 
      resolve: { 
       fb_like: 'fbLike' 
      } 
     }) 
     .state('like_gate', { 
      url: '/like_gate', 
      templateUrl: 'like_gate.html', 
      controller: 'likeGateCtrl' 
     }) 
     .state('terms_and_conditions', { 
      url: '/terms_and_conditions', 
      templateUrl: 'terms_and_conditions.html', 
      controller: 'termsAndConditionsCtrl' 
     }); 

     $urlRouterProvider.otherwise("/home"); 

     //$locationProvider.hashPrefix('!'); 

}) 

.run(function($rootScope){ 

    $rootScope 
     .$on('$stateChangeStart', 
      function(event, toState, toParams, fromState, fromParams){ 
       console.log("State Change: transition begins!"); 
     }); 

    $rootScope 
     .$on('$stateChangeSuccess', 
      function(event, toState, toParams, fromState, fromParams){ 
       console.log("State Change: State change success!"); 
     }); 

    $rootScope 
     .$on('$stateChangeError', 
      function(event, toState, toParams, fromState, fromParams){ 
       console.log("State Change: Error!"); 
     }); 

    $rootScope 
     .$on('$stateNotFound', 
      function(event, toState, toParams, fromState, fromParams){ 
       console.log("State Change: State not found!"); 
     }); 

    $rootScope 
     .$on('$viewContentLoading', 
      function(event, viewConfig){ 
       console.log("View Load: the view is loaded, and DOM rendered!"); 
     }); 

    $rootScope 
     .$on('$viewcontentLoaded', 
      function(event, viewConfig){ 
       console.log("View Load: the view is loaded, and DOM rendered!"); 
     }); 

}) 

.controller('homeCtrl', function($scope, fbLike){ 

    $scope.fbLike = fbLike.liked(); 

}) 

.controller('enterCompetitionCtrl', function($scope, fbLike){ 

    fbLike.liked(); 

}) 

.controller('likeGateCtrl', function($scope){ 

}) 

.controller('termsAndConditionsCtrl', function($scope){ 

}) 

.factory('fbLike', function($http, $q){ 

    return { 

     liked: function(){ 
      return true; 
     } 

    }; 

}); 

任何人都經歷可以看看嗎?

感謝:d

回答

15

它看起來你有$viewcontentLoaded而不是$viewContentLoaded。你忘了駱駝案大寫的C

+3

非常感謝!我完全沒有注意到它對$ viewContentLoaded很好用,沒有注意到:) – punkbit

+0

當dom被添加但角度已經完成處理dom之前,這個事件被激發。你可以通過添加一個id或class作爲角度變量來測試,並嘗試在$ viewContentLoaded和jQuery中找到它。你不會找到它。 – Mussammil