2016-02-26 96 views
2

請, 我在我的項目中使用ui-router。當用戶在狀態之間導航時,$stateChangeStart事件正常工作。 但是,如果我刷新瀏覽器,它不起作用。

這是我的源代碼:

(function(){ 
'use strict'; 

angular.module('app.overlay') 
    .directive('ibLoading', ['$rootScope' , '$timeout', loading]); 

    function loading($rootScope , $timeout){ 
     console.log("In dirrective"); 
     var directive = { 
      restrict:'EAC', 
      link:link 
     }; 

     function link(scope, element) { 

      $rootScope.$on('$stateChangeStart', function() { 
        $timeout(function(){ 
         element.addClass('show'); 
        } , 50); 
       }); 

       $rootScope.$on('$stateChangeSuccess', function() { 
        $timeout(function(){ 
         element.removeClass('show'); 
        } , 700); 
       }); 

     } 

     return directive; 
    } 
})(); 

我的佈局文件

<div ng-controller="ShellController as vm"> 
<header class="clearfix"> 
    <ht-top-nav navline="vm.navline"></ht-top-nav> 
</header> 
<section id="content" class="content"> 
    <!--<div ng-include="'app/layout/sidebar.html'"></div>--> 
    <div ui-view></div> 
    <div class="ib-loading">Loading ...</div> 
</section> 
</div> 
+0

你能否提供一些代碼的例子嗎? –

+0

這將取決於它在哪裏使用。沒有任何代碼,沒有人可以幫助你。加載頁面時,請提供 – charlietfl

+0

你期待指令寄存器的初始變化相關的代碼?它不會。仍然不清楚問題是什麼或預期的行爲 – charlietfl

回答

0

試試這個,
在這裏,如果你改變你的狀態,你可以看到在控制檯的狀態,但如果你刷新你的頁面將重新加載狀態

$rootScope.$on('$stateChangeStart', function (event, toState, toParams) { 
    console.log("Switching To: ", toState.name); 
}); 
1

用戶刷新不會觸發$ stateChangeStart或$ stateChangeSuccess。但是$ viewContentLoading$ viewContentLoaded確實會觸發。

所以,你可以嘗試以下方法:

  $rootScope.$on('$viewContentLoading', function() { 
       $timeout(function(){ 
        element.addClass('show'); 
       } , 50); 
      }); 
      $rootScope.$on('$viewContentLoaded', function() { 
       $timeout(function(){ 
        element.removeClass('show'); 
       } , 700); 
      }); 
+0

是的,但你不能抓住這些國家,或有沒有辦法? – Nick