我正在尝试将服务注入如下指令:
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; } } }]);