2015-08-28 358 views
1

我是新来angularjs,我注入D3在main.js如下:如何注入d3依赖?

 function (angular) { 
      'use strict'; 

     angular.module('main', ['ngFileUpload', 'ngCookies', 'ui.router', 'd3']) 
     .constant('_', window._); 

     })(window.angular); 

,但我得到一个错误:

 Uncaught Error: [$injector:modulerr] 
     Failed to instantiate module main due to: 
      Error: [$injector:modulerr] Failed to instantiate module d3 due to: 
     Error: [$injector:nomod] Module 'd3' is not available! You either    misspelled the module name or forgot to load it. If registering a module ensure    that you specify the dependencies as the second argument. 
+0

是D3是您的角度指令?或只是lib js文件? –

+1

只需添加lib添加到您的索引D3是没有棱角的指令或模块可以不加这种方式,但如果你有JS加了你可以在你的控制器 –

回答

2

如果使用正常D3库你不注入角度依赖。

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script> 

您可以使用d3.js角指令或D3 “包装” 角指令,我建议你:

+0

使用D3没有我使用d3.js – Hassan

+0

然后你不需要注入“D3 '在你的angular.module中。您只需要在index.html中导入d3.min.js脚本,并且可以随处调用d3.js函数。 –

+0

@IvanCoronado,我一直在注入它,并且该服务还将上述脚本添加到DOM,并在脚本加载时返回带有原始D3对象的承诺。这不能保证D3可以使用,如果你依赖全局D3对象,它可能不会被拉下来吗? – mtpultz