角:如何使用一个决心,我的应用程序的所有路由 [英] Angular : How use one resolve for all the routes of my application

查看:104
本文介绍了角:如何使用一个决心,我的应用程序的所有路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解决谁加载当前用户我的应用程序的所有页面的承诺。
现在我再说一遍决心在每个$ routeProvider.when()我的路线。

I would like resolve the promise who load the current user for all the page of my application. Now I repeat the resolve in each $routeProvider.when() of my routes.

  $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();
      }
  }
 });

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

My goal is to resolve my current user for all the routes without repetition.

推荐答案

您总是可以包在 $ routeProvider 用你自己的实现。

You could always wrap the existing when method on the $routeProvider with your own implementation.

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);
      };   

   }]);

您可能要添加一些错误在那里检查,并使用类似下划线默认 方法,而不是只覆盖现有resolve属性,但至少这样可以保证你所有的路线将有你想要的东西。

You probably want to add some error checking in there, and use something like underscores defaults method instead of just overwriting the existing resolve property, but at least this way you can guarantee all your routes will have what you want.

这也是很容易的包装这成一个辅助方法。

It's also easy enough to wrap this up into a helper method.

这篇关于角:如何使用一个决心,我的应用程序的所有路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆