我正在尝试将服务注入如下指令:
var app = angular.module('app',[]); app.factory('myData', function(){ return { name : "myName" } }); app.directive('changeIt',function($compile, myData){ return { restrict: 'C', link: function (scope, element, attrs) { scope.name = myData.name; } } });
但是,这给了我一个错误Unknown provider: myDataProvider。有人可以调查一下代码,并告诉我是否做错了什么?
Unknown provider: myDataProvider
您可以对指令进行注入,看起来就像在其他地方一样。
app.directive('changeIt', ['myData', function(myData){ return { restrict: 'C', link: function (scope, element, attrs) { scope.name = myData.name; } } }]);