angularjs - 如何在 AngularJS 中将服务注入(inject) app.config

标签 angularjs service config

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

authService.getLoggedin() 根据用户是否登录返回 false 或 true。如果不允许,我不想让他们访问 URL。

但我得到这个错误:
错误:[$injector:modulerr] 无法实例化模块 wikiApp,原因如下:
[$injector:unpr] 未知提供者:authService

最佳答案

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

    因此,要在配置中注入(inject)服务,您只需要通过在其名称中添加“提供者”来调用服务的提供者。
    angular.module('myApp')
      .service('FooService', function(){
        //...etc
      })
      .config(function(FooServiceProvider){
        //...etc
      });
    

    关于angularjs - 如何在 AngularJS 中将服务注入(inject) app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22682753/

    相关文章:

    c# - 在运行时更改其他项目的 app.config 值

    xml - Google Apps 检索用户信息

    javascript - 将用户输入的输入数字限制为 4 位

    Linux远程启动/停止过程

    azure - 读取 azure blob 时使用服务主体

    c# - 在windows服务项目中注册autofac

    sharepoint 2010 中的身份验证提供程序 'greyed out'(启用匿名访问)

    javascript - AngularJs 指令 : How to dynamically set attributes on ng-repeat element

    javascript - AngularJS - 如何重构我的 Jasmine 规范?

    javascript - UI-Grid 在页面加载时不会占用 100% 的宽度