AngularJS路由之ui-router(三)

前端之家收集整理的这篇文章主要介绍了AngularJS路由之ui-router(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、为ui-router添加进度条

在使用动态控制器或者ajax,添加数据的时候需要进度条提示

我们可以使用路由状态的事件添加全局进度条提示

$stateChangeStart: 当状态开始改变时触发

$stateChangeSuccess: 当状态改变结束时触发

二、实例1,创建一个进度条指令

  1. // Route State Load Spinner(used on page or content load)
  2. MetronicApp.directive('ngSpinnerBar',['$rootScope','$state',function($rootScope,$state) {
  3. return {
  4. link: function(scope,element,attrs) {
  5. // by defult hide the spinner bar
  6. element.addClass('hide'); // hide spinner bar by default
  7.  
  8. // display the spinner bar whenever the route changes(the content part started loading)
  9. $rootScope.$on('$stateChangeStart',function() {
  10. element.removeClass('hide'); // show spinner bar
  11. });
  12.  
  13. // hide the spinner bar on rounte change success(after the content loaded)
  14. $rootScope.$on('$stateChangeSuccess',function() {
  15. element.addClass('hide'); // hide spinner bar
  16. $('body').removeClass('page-on-load'); // remove page loading indicator
  17. Layout.setAngularJsSidebarMenuActiveLink('match',null,$state); // activate selected link in the sidebar menu
  18. // auto scorll to page top
  19. setTimeout(function () {
  20. App.scrollTop(); // scroll to the top on content load
  21. },$rootScope.settings.layout.pageAutoScrollOnLoad);
  22. });
  23.  
  24. // handle errors
  25. $rootScope.$on('$stateNotFound',function() {
  26. element.addClass('hide'); // hide spinner bar
  27. });
  28.  
  29. // handle errors
  30. $rootScope.$on('$stateChangeError',function() {
  31. element.addClass('hide'); // hide spinner bar
  32. });
  33. }
  34. };
  35. }
  36. ])

更多:

AngularJS路由之ui-router(二)

AngularJS路由之ui-router(一)

猜你在找的Angularjs相关文章