微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – Angular:如何使用一个解决我的应用程序的所有路由

我想解决承诺加载当前用户为我的应用程序的所有页面
现在我重复每个$ routeProvider.when()我的路由的解决方案。
$routeProvider.when('/users/edit/:userId',{
  templateUrl: 'app/app/assets/partials/user-edit.html',controller: 'UserEditController',resolve:{  
    'currentUser':function( UserService){            
        return UserService.getCurrentUser();
      }
  }
  });  

  $routeProvider.when('/staff_profil/edit',{
  templateUrl: 'app/app/assets/partials/profil-edit.html',controller: 'ProfilEditController',resolve:{  
    'currentUser':function( UserService){            
        return UserService.getCurrentUser();
      }
  }
 });

我的目标是解决我的当前用户的所有路由,没有重复。

你可以随时用 $routeProvider自己的实现包装现有的when方法
var myModule = angular.module('myModule',['ngRoute'])
   .config(['$routeProvider',function($routeProvider){

      var originalWhen = $routeProvider.when;

      $routeProvider.when = function(path,route){

         route.resolve = {
            'currentUser':function( UserService){            
              return UserService.getCurrentUser();
            }
         };

         return originalWhen(path,route);
      };   

   }]);

你可能想添加一些错误检查在那里,并使用像underscores defaults方法,而不是只覆盖现有的解决方属性,但至少这样,你可以保证所有的路由将有你想要的。

这也很容易包装成一个帮助方法

原文地址:https://www.jb51.cc/angularjs/145304.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐