angularjs模块依赖 [英] angularjs module dependencies

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

问题描述



我已经定义了我的主要模块:

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

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



如果我这样做:

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

然后我收到一个错误'no module:breeze'。



如果我这样做:



< pre class =lang-js prettyprint-override> angular.module('domiciliations.service',['ngResource'])
factory('Domiciliation',function($ resource,微风,记录器){
}

那么我想如何指定依赖于微风和记录器?

解决方案

breeze 不是一个模块 - 它是一个价值(服务简写)在<$ code code code code code code code code code code $ b

当你做:

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

您配置 domiciliations.service 模块与依赖于模块 ngResource breeze loggerService 。角色找不到 breeze 模块并抛出异常。



假设 loggerService 是一个模块, logger 是本模块中的一个服务,以下应该可以工作( breeze logger 将注入工厂函数):

  angular.module('domiciliations.service',['ngResource','loggerService'])。 
工厂('个人资料',''资源','微风','logger',
功能($资源,微风,记录器){
}
])


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?

If I do:

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 is not a module - it's a value (shorthand for service) in the domiciliations module: value('breeze', window.breeze);.

When you do:

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

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.

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天全站免登陆