angularjs 模块依赖 [英] angularjs module dependencies

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

问题描述

我已经这样定义了我的主模块:

I've defined my main module as such:

angular.module('domiciliations', ['domiciliations.service', 'loggerService', 'person.directives']).
  config(['$routeProvider', function ($routeProvider) {
  $routeProvider.
      when('/domiciliations/mandats', { templateUrl: 'domiciliations/views/mandats.html', controller: mandatsCtrl }).
      when('/domiciliations/mandats/:rum', { templateUrl: 'domiciliations/views/mandat.html', controller: mandatCtrl }).
      otherwise({ redirectTo: '/domiciliations/mandats' });
  }]).
  value('toastr', window.toastr).
  value('breeze', window.breeze);

我的问题是如何在我的控制器中指定模块依赖项?

My problem is how to how specify module dependencies in my controller?

如果我这样做:

angular.module('domiciliations.service', ['ngResource', 'breeze', 'loggerService']).
  factory('Domiciliation', function ($resource, breeze, logger) {
}

然后我得到一个错误没有模块:微风".

Then I get an error 'no module: breeze'.

如果我这样做,它会起作用:

It works if I do:

angular.module('domiciliations.service', ['ngResource']).
   factory('Domiciliation', function ($resource, breeze, logger) {
}

那么我应该如何指定对微风和记录器的依赖关系?

So how am I suppose to specify dependencies on breeze and logger?

推荐答案

breeze 不是一个模块 - 它是一个 值 (domiciilations 模块中服务的简写):value('breeze', window.breeze);.

breeze is not a module - it's a value (shorthand for service) in the domiciliations module: value('breeze', window.breeze);.

当你这样做时:

angular.module('domiciliations.service', ['ngResource', 'breeze', 'loggerService']).
  factory('Domiciliation', function ($resource, breeze, logger) {
}

您配置 domiciilias.service module 与模块 ngResourcebreeze 的依赖项记录器服务.Angular 找不到 breeze module 并抛出异常.

You configure the domiciliations.service module with dependencies to the modules ngResource, breeze and loggerService. Angular can't find the breeze module and throws an exception.

假设 loggerService 是一个模块并且 logger 是这个模块中的一个服务,以下应该可以工作(breezelogger 将被注入到工厂函数中):

Assuming loggerService is a module and logger is a service in this module, the following should work (breeze and logger will get injected in the factory function):

angular.module('domiciliations.service', ['ngResource','loggerService']).
   factory('Domiciliation', ['$resource','breeze','logger',
     function ($resource, breeze, logger) {
    }
  ])

这篇关于angularjs 模块依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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