Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I am trying to inject a service into a directive like below:

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; 

});

But this is returning me an error Unknown provider: myDataProvider. Could someone please look into the code and tell me if I am doing something wrong?

1 Answer

0 votes
by (106k points)
edited by

For injecting a service into a directive in AngularJS you can use the below-mentioned code:-

Looking for Angularjs material from basics! Refer to this video on Angularjs provided by Intellipaat:

app.directive('changeIt', ['myData', function(myData){ 

return { 

restrict: 'C', 

link: function (scope, element, attrs) { 

scope.name = myData.name; 

}]);

Related questions

+2 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+2 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...