Inject service into directive

时间:2015-09-01 22:43:44

标签: angularjs angularjs-directive

Cant inject the comment service into comment directive getting errorComment service not found

// Code goes here

var noBlogApp = angular.module("noBlogApp", ['ngRoute', 'ngSanitize', 'commentSystem']);


noBlogApp.config(['routeProvider', function(routeProvider) {
   routeProvider.null when('/', {
   controller: "noBlogController", null templateUrl: 'post.html',

  }).when('/post/:id', {
    controller: 'singlePostController',
    templateUrl: 'singlepost.html',

  });
}]);


noBlogApp.service('commentService', ['http', function(http) {
  http.get('js/assureBlog.JSON').success(function(data) {

    this.getComment = function(id) {
      scope.comment = data[id];
      return scope.comment;
    }

    var addComment = function() {
    }

    var addReply = function() {
    }

   var deleteComment = function() {

    }

    var deleteReply = function() {

    }

  });

}]);

noBlogApp.controller('noBlogController', ['scope', 'http', function(scope, http) {
  scope.message = "hello";
   http.get('js/assureBlog.JSON').success(function(data) {
     scope.post = data;

  });

}]);
nullnoBlogApp.controller('singlePostController', ['scope', 'http', 'routeParams', 'commentService', function(scope, http, routeParams, commentService) {
  null http.get('js/assureBlog.JSON').success(function(data) {
     scope.post = data[routeParams.id];

  });
  scope.commentService = commentService;
  console.log("this is the" + commentService) null
}]);

noBlogApp.directive('comments', ["commentService", function(commentService) {

  return {
    scope: {
        id: '@'
      },
      template: id: {
        {
          id
        }
      }
    link: function(scope, element, attrs, commentService) {
       console.log(scope.commentService);
    }

  };

}]);

1 个答案:

答案 0 :(得分:0)

Although the question is poorly formatted, it looks like your issue is this:

link:function(scope,element,attrs,commentService)

Remove the commentService from the link function. You've already added the dependency to the directive construction.