It's impossible. The best you can do is associate the scope of the directive with the function of the parent scope, which returns an instance of your service:
app.directive('changeIt', function(){ return { restrict: 'CA', scope: { getDataFn : '&' }, link: function (scope) { scope.name = getDataFn().name; } } });
and then, in your opinion:
<div class='change-it' get-data-fn='getMyData()'></div> <div class='change-it' get-data-fn='getYourData()'></div>
Finally, you need to add getMyData() and getYourData() to the parent scope:
app.controller('Ctrl', function($scope, myData, yourData) { $scope.getMyData = function() { return myData; }; $scope.getYourData = function() { return yourData; }; });
Plunker script here .
I can come up with another approach: you can create an abstract factory and enter it into a directive, and then pass the parameter to the directive so that it can tell the abstract factory to create the correct service. Something like that:
app.service('dataFactory', function(myData, yourData) { this.create = function(type) { if (type === 'myData') return myData; else if (type === 'yourData') return yourData; }; }); app.directive('changeIt', function(dataFactory){ return { restrict: 'CA', scope: true , link: function (scope, element, attrs) { scope.name = dataFactory.create(attrs.type).name; } } });
And now you need to pass the type to the directive:
<div class='change-it' type="myData"></div> <div class='change-it' type="yourData"></div>
Plunker is here .