Angularjs 在传递到可重用函数的过程中进行解析

Angularjs 在传递到可重用函数的过程中进行解析,angularjs,Angularjs,我在resolve函数中有很多相同的代码。是否可以为其创建一个可重用的函数并将其传递给resolve FROM THIS... .when('/workers', { resolve: { "check": function () { if (!isLoggedIn() || !isAdmin()) { window.location.href = '#/about';

我在resolve函数中有很多相同的代码。是否可以为其创建一个可重用的函数并将其传递给resolve

FROM THIS... 

.when('/workers', {
        resolve: {
            "check": function () {
                if (!isLoggedIn() || !isAdmin()) {
                    window.location.href = '#/about';
                }
            },
        },
        templateUrl: 'html/admin/workers.html',
        controller: 'adminWorkersController'
    })

  TO SOMETHING LIKE THIS: 

 .when('/workers', {
            resolve: myResolveFunction()
            templateUrl: 'html/admin/workers.html',
            controller: 'adminWorkersController'
        })

您可以将服务用于可重用代码。例如:

resolve: {
    "check": function(yourService) {//inject service
        yourService.method(); //this method of the service contains reusable code
    }
}

您可以将服务用于可重用代码。例如:

resolve: {
    "check": function(yourService) {//inject service
        yourService.method(); //this method of the service contains reusable code
    }
}

您可以为路由解析创建提供程序

var app = angular.module('app', []);
        //Must be a provider since it will be injected into module.config()
        app.provider('routeResolver', function () {
            this.$get = function () {
                return this;
            };
            this.route = function () {
               var resolve = function () {
               // resolve 
               } 
              return {resolve: resolve};
           };
        });

app.config( function(routeResolverProvider) {
  .when('/workers', {
            resolve: routeResolverProvider.resolve()
            templateUrl: 'html/admin/workers.html',
            controller: 'adminWorkersController'
        })
})

您可以为路由解析创建提供程序

var app = angular.module('app', []);
        //Must be a provider since it will be injected into module.config()
        app.provider('routeResolver', function () {
            this.$get = function () {
                return this;
            };
            this.route = function () {
               var resolve = function () {
               // resolve 
               } 
              return {resolve: resolve};
           };
        });

app.config( function(routeResolverProvider) {
  .when('/workers', {
            resolve: routeResolverProvider.resolve()
            templateUrl: 'html/admin/workers.html',
            controller: 'adminWorkersController'
        })
})