2015-08-28 328 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