我一直在尝试将D3.js与Angular集成,并遵循本教程:
http://www.ng-newsletter.com/posts/d3-on-angular.html
本教程创建了一个包含d3Service的d3模块,并将其注入到指令中.我的应用程序结构略有不同,但每当我尝试注入d3服务时,它在我的指令链接函数中显示为未定义.我可以毫无问题地将d3服务注入我的控制器.这是我正在做的事情:
app.js:
var sentimentApp = angular.module('sentimentApp',[
'ngRoute','ngSanitize','sentimentAppServices','sentimentAppDirectives','sentimentAppControllers'
]);
在services.js中,我有几个服务,其中一个是d3:
var sentimentAppServices = angular.module('sentimentAppServices',['ngResource'])
// other services
.factory('d3',[function(){
var d3;
d3 = // d3 library code here
return d3;
}]);
现在在directives.js中:
var sentimentAppDirectives = angular.module('sentimentAppDirectives',['sentimentAppServices']);
sentimentAppDirectives.directive('lsPieChart',['d3',function($compile,d3){
return {
// scope & template
link: function($scope,elem,attr,ctrl) {
console.log(d3); // undefined
}
}
有小费吗?谢谢.
最佳答案