在配置注入依赖()模块 - AngularJS [英] Injecting Dependencies in config() modules - AngularJS

查看:174
本文介绍了在配置注入依赖()模块 - AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在app.js我有以下途径:

Currently in app.js i have the following routes:

var gm = angular.module('gm', ['gm.services','gm.directives','gm.filters','gm.controllers','ngSanitize']);

gm.config(['$routeProvider', 'Path', function($routeProvider, Path) {

    $routeProvider.when('/login', { 
        templateUrl: Path.view('application/authentication/login.html'), 
        controller: 'authController' 
    });

    $routeProvider.when('/dashboard', { 
        templateUrl: Path.view('application/dashboard/index.html'), 
        controller: 'dashboardController' 
    }); 

    $routeProvider.otherwise({ 
        redirectTo: '/login'
    });

}]);

我试着去在拍摄对象的路径依赖,你可以看到。虽然我得到一个错误说它不能找到这个供应商。我想这是因为配置模块provicers的任何事情之前先execued。下面是我的services.js路径提供高清

Im trying to in in ject the Path dependency as you can see. Although i get an error saying it cant find this provider. I think this is because config module provicers are execued first before anything else. below is my Path provider definition in services.js

gm.factory("Path", function() {
  return {
    view: function(path) {
      return 'app/views/' + path; 
    },
    css: function(path) {
      return 'app/views/' + path; 
    },
    font: function(path) {
      return 'app/views/' + path; 
    },
    img: function(path) {
      return 'app/views/' + path; 
    },
    js: function(path) {
      return 'app/views/' + path; 
    },
    vendor: function(path) {
      return 'app/views/' + path; 
    },
    base: function(path) {
      return '/' + path; 
    }
  }
}); 

我怎么能注入此提供成配置模块?

how can i inject this provider into a config module?

推荐答案

的.config 您只能使用供应商(如 $ routeProvider )。在 .RUN 您只能使用服务的实例(例如 $路线)。你有一个工厂,而不是供应商。 看到这个片段与创建此的三种方式:服务,工厂和提供商
他们也提到这一点在角文档 https://docs.angularjs.org/guide/services

In .config you can only use providers (e.g. $routeProvider). in .run you can only use instances of services (e.g. $route). You have a factory, not a provider. See this snippet with the three ways of creating this: Service, Factory and Provider They also mention this in the angular docs https://docs.angularjs.org/guide/services

这篇关于在配置注入依赖()模块 - AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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