如何将服务注入AngularJS中的app.config

use*_*894 10 service config angularjs

wikiApp.config(['$routeProvider','authService', 
  function($routeProvider,authService) {
var admin = authService.getLoggedin();

$routeProvider
    .when('/hjem',{
        templateUrl: 'partials/homeContent.html',
    admin: false
    })
  .when('/articles/:article',{
    templateUrl: 'partials/articles.html',
    admin: false
  })
  .when('/newArticle',{
    templateUrl: 'partials/postArticle.html',
    controller: 'articleController',
    admin: true
  })
Run Code Online (Sandbox Code Playgroud)

authService.getLoggedin()返回false或true,具体取决于用户是否登录.如果他们不被允许,我想不允许他们到Url.

但我收到此错误:错误:[$ injector:modulerr]由于以下原因无法实例化模块wikiApp:[$ injector:unpr]未知提供者:authService

vil*_*ole 28

  1. angular.config只接受Providers
  2. 每个服务,工厂等都是Provider的实例

因此,要在配置中注入服务,您只需要通过在其名称中添加"提供者"来调用服务的提供者.

angular.module('myApp')
  .service('FooService', function(){
    //...etc
  })
  .config(function(FooServiceProvider){
    //...etc
  });
Run Code Online (Sandbox Code Playgroud)

  • 只是一个额外的信息.您可以按如下方式使用FooService:FooServiceProvider.$ get.NameOfFunctionInService(如果适用,则为perimeters) (6认同)
  • 只是想强调我必须使用`$ get()`因为这是一个函数:`FooServiceProvider.$ get().NameOfFunctionInService(如果适用,则为perimeters)`.不确定是否有变化或是否是错字.无论如何,谢谢你们! (4认同)

Ale*_*hin 18

在配置阶段你只能要求提供者($ routeProvider,$ locationProvider等),这意味着你不能注入任何其他实例,所以我建议你在运行阶段注入你的服务,那里你将有一个服务实例.

// configuration
app.config(function($routeProvider) {

});

//inject any instance 
 app.run(function($rootScope,authService) {
  var admin = authService.getLoggedin();

  $rootScope.$on('$routeChangeStart', function(next, current) { 
     // your logic here...
  }); 
});
Run Code Online (Sandbox Code Playgroud)


Mah*_*alt 5

如果你想调用一个外部函数(在你的情况下是服务函数)形成你的路由(.config),如下所示:templateProvider.getTemplate('about')

.state('index.about', {  

    url: "/about",  
    templateUrl: templateProvider.getTemplate('about'),  
    controller: 'AboutCtrl',  
    controllerAs: 'about',  
    data: {pageTitle: 'About Us Page'}  

})  
Run Code Online (Sandbox Code Playgroud)

您无法为此创建服务或工厂.相反,您必须创建一个提供者.

以下是从名称生成模板路径的Provider的真实示例:

(function () {  

    'use strict';  
    angular  

        .module('mega-app')  

        .provider('template', provider);  

      function provider(CONSTANT) {  

        // The provider must include a $get() method This $get() method  
        // will be invoked using $injector.invoke() and can therefore use  
        // dependency-injection.  
       this.$get = function () {  

            return {}  

        };  
       /**  
         * generates template path from it's name  
         *  
         * @param name  
         * @returns {string}  
         */  
       this.getTemplate = function (name) {  

            return CONSTANT.TEMPLATES_URL + name + '/' + name + '.html';  
        }  


        /**  
         * generates component path from it's name  
         * @param name  
         * @returns {string}  
         */  
       this.getComponent = function (name) {  

            return CONSTANT.COMPONENTS_URL + name + '.html';  
        }  

    };  
})();  
Run Code Online (Sandbox Code Playgroud)

在路由(.config)中使用此类Provider将如下所示:

(function () {  

    'use strict';  
    angular  

        .module('mega-app')  

        .config(routes);  
   function routes($stateProvider, $urlRouterProvider, templateProvider) {  



       $stateProvider  
            //----------------------------------------------------------------  
            // First State  
            //----------------------------------------------------------------  
            .state('index', {  

                abstract: true,  
                url: "/index",  
                templateUrl: templateProvider.getComponent('content'),  
                controller: 'IndexCtrl',  
                controllerAs: 'index',  
            })  

            //----------------------------------------------------------------  
            // State  
            //----------------------------------------------------------------  
            .state('index.home', {  

                url: "/home",  
                templateUrl: templateProvider.getTemplate('home'),  
                controller: 'HomeCtrl',  
                controllerAs: 'home',  
                data: {pageTitle: 'Home Page'}  

            })  

            //----------------------------------------------------------------  
            // State  
            //----------------------------------------------------------------  
            .state('index.about', {  

                url: "/about",  
                templateUrl: templateProvider.getTemplate('about'),  
                controller: 'AboutCtrl',  
                controllerAs: 'about',  
                data: {pageTitle: 'About Us Page'}  

            })  

        //----------------------------------------------------------------  
        // Default State  
        //----------------------------------------------------------------  
       $urlRouterProvider.otherwise('/index/home');  
    };  
})();  
Run Code Online (Sandbox Code Playgroud)

VIP注意:

要注入提供者,你必须使用xxxProvider对其进行后缀(提供者的名称不应该被后缀,只能在.config中注入).