2015-04-05 77 views
0

我想實現一個系統,在路由更改後,我只想運行一次基於JavaScript的動畫。發生的是,現在如果你在頁面之間切換,並返回到關於頁面。腳本真的減慢了網站,這正是我想要避免的。路由更改後,運行js腳本一次

app.controller( 'aboutController',函數($範圍,$路徑){

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


    // Just need this to run once 
    $('#about').particleground({ 
     dotColor: '#666', 
     lineColor: '#666', 
     lineWidth: .3, 
     particleRadius: 3, 
     parallaxMultiplier: 3 
    }); 


}); 

回答

1

檢查currentRoute.redirectTo運行$ routeChangeSuccess`裏面的動畫之前,它會阻止動畫運行多次。

$scope.$on("$routeChangeSuccess", function(event, currentRoute, previousRoute){ 
    if(!currentRoute.redirectTo) { 
       $('#about').particleground({ 
       dotColor: '#666', 
       lineColor: '#666', 
       lineWidth: .3, 
       particleRadius: 3, 
       parallaxMultiplier: 3 
      }); 
     } 
}); 

爲什麼它執行兩次的原因是:

在HTML:

<a href="#about">›&nbsp;&nbsp;About Me</a> 

在href中,您只有#about但它必須是#/about

<a href="#/about">›&nbsp;&nbsp;About Me</a> 
+0

仍然重複..你可以看到測試頁在:devncode.com/#/about – Shivam 2015-04-05 07:45:43

+0

更新了我的答案。在你的菜單鏈接href你有'#關於'它必須是'#/ about' – mohamedrias 2015-04-05 09:02:57

+0

更新href值,但它仍然導致同樣的問題 – Shivam 2015-04-05 15:28:16