最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS: inject service into directive? - Stack Overflow

programmeradmin3浏览0评论

I've been trying to integrate D3.js with Angular, and am following this tutorial: .html

The tutorial creates a d3 module which contains d3Service, and that gets injected into a directive. My app has a slightly different structure, but whenever I try to inject the d3 service, it shows up as undefined in my directive link function. I can inject the d3 service into my controller without issue. Here's what I'm doing:

app.js:

var sentimentApp = angular.module('sentimentApp', [
  'ngRoute',
  'ngSanitize',
  'sentimentAppServices',
  'sentimentAppDirectives',
  'sentimentAppControllers'
]);

Within services.js, I have several services, one of which is d3:

var sentimentAppServices = angular.module('sentimentAppServices', ['ngResource'])
// other services
.factory('d3', [function(){
  var d3;
  d3 = // d3 library code here
  return d3; 
}]);

Now in directives.js:

var sentimentAppDirectives = angular.module('sentimentAppDirectives', ['sentimentAppServices']);

sentimentAppDirectives.directive('lsPieChart', ['d3', function($pile, d3){
   return {
     // scope & template
     link: function($scope, elem, attr, ctrl) {
       console.log(d3); // undefined
     }
   }

Any tips? Thanks.

I've been trying to integrate D3.js with Angular, and am following this tutorial: http://www.ng-newsletter./posts/d3-on-angular.html

The tutorial creates a d3 module which contains d3Service, and that gets injected into a directive. My app has a slightly different structure, but whenever I try to inject the d3 service, it shows up as undefined in my directive link function. I can inject the d3 service into my controller without issue. Here's what I'm doing:

app.js:

var sentimentApp = angular.module('sentimentApp', [
  'ngRoute',
  'ngSanitize',
  'sentimentAppServices',
  'sentimentAppDirectives',
  'sentimentAppControllers'
]);

Within services.js, I have several services, one of which is d3:

var sentimentAppServices = angular.module('sentimentAppServices', ['ngResource'])
// other services
.factory('d3', [function(){
  var d3;
  d3 = // d3 library code here
  return d3; 
}]);

Now in directives.js:

var sentimentAppDirectives = angular.module('sentimentAppDirectives', ['sentimentAppServices']);

sentimentAppDirectives.directive('lsPieChart', ['d3', function($pile, d3){
   return {
     // scope & template
     link: function($scope, elem, attr, ctrl) {
       console.log(d3); // undefined
     }
   }

Any tips? Thanks.

Share Improve this question edited Jan 21, 2014 at 12:13 VividD 10.5k8 gold badges66 silver badges112 bronze badges asked Jan 19, 2014 at 0:29 bjudsonbjudson 4,0833 gold badges31 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

The problem is that your hinted dependencies don't match up to what you're actually passing in:

['$pile, d3', function($pile, d3

So, what you were doing is passing the d3 service as the variable $pile and not passing anything as the variable d3.

It might help you to understand what this is for. In non-minified code, you could take out that array wrapper altogether, like this:

app.directive('directiveName', function($pile, d3) {
  // ....
});

The point of passing the names as a string is because strings won't be affected by minification. This means that angular will know how to inject the right dependencies in a case like this:

 ['$pile, d3', function(a, b

a will be set to the $pile service and b to your d3 service.

发布评论

评论列表(0)

  1. 暂无评论