2016-09-22 35 views

回答

1

你可以使用以前的路線的變化和當前路由變化之間的時間差...掛鉤到$stateChangeSuccess事件到$rootScope

的代碼將類似如下(在你的根控制器添加它//應用級控制器)

 var timeStart; 

    $rootScope.$on('$stateChangeSuccess', function(current, prev){ 
      if(angular.isUndefined(timeStart)){ 
       $log.info('this is the first route') 
      } else { 
       var timeSpent = moment().diff(timeStart).format('mm:ss'); 
       $log.info(`total time spent ${timeSpent} route ${prev.name}`); 
       timeStart = moment(); 
      } 
    }) 

**編輯**

它使用momentjs,您可以使用日期操作任何日期庫/獲取時間差,read here如何做到這一點

+0

很好的答案,但你在這裏使用'momentjs'功能,並且PO沒有用'momentjs'提出解決方案。你應該在這裏的答案中提到。解決方案雖然+1。它增加了一些好的東西。 – Paritosh

+0

@entre,有沒有沒有使用momentjs – RAMESHKUMAR

+0

任何解決方案請通過編輯中的鏈接,找出如何使用核心JavaScript日期對象進行日期操作 – harishr

相關問題