加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:29:38 所属栏目:安全 来源:网络整理
导读:我想解决承诺加载当前用户为我的应用程序的所有页面。 现在我重复每个$ routeProvider.when()我的路由的解决方案。 $routeProvider.when('/users/edit/:userId',{ templateUrl: 'app/app/assets/partials/user-edit.html',controller: 'UserEditController',
我想解决承诺加载当前用户为我的应用程序的所有页面。
现在我重复每个$ 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方法,而不是只覆盖现有的解决方案属性,但至少这样,你可以保证所有的路由将有你想要的。

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读