Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's not possible that way. The best you can do is bind the directive scope to a function of its parent scope that returns an instance of your service:</p> <pre><code>app.directive('changeIt', function(){ return { restrict: 'CA', scope: { getDataFn : '&amp;' }, link: function (scope) { scope.name = getDataFn().name; } } }); </code></pre> <p>and then in your view:</p> <pre><code>&lt;div class='change-it' get-data-fn='getMyData()'&gt;&lt;/div&gt; &lt;div class='change-it' get-data-fn='getYourData()'&gt;&lt;/div&gt; </code></pre> <p>Finally, you need to add <code>getMyData()</code> and <code>getYourData()</code> to the parent scope:</p> <pre><code>app.controller('Ctrl', function($scope, myData, yourData) { $scope.getMyData = function() { return myData; }; $scope.getYourData = function() { return yourData; }; }); </code></pre> <p>Plunker script <a href="http://plnkr.co/edit/IdHeIiTn2TiDuovlIG9L?p=preview">here</a>.</p> <p>I can think of another approach, though: you could create an abstract factory and inject it into the directive, and then pass a parameter to the directive so it could tell the abstract factory to create the correct service. Something like this:</p> <pre><code>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; } } }); </code></pre> <p>And now you need to pass the type to the directive:</p> <pre><code>&lt;div class='change-it' type="myData"&gt;&lt;/div&gt; &lt;div class='change-it' type="yourData"&gt;&lt;/div&gt; </code></pre> <p>Plunker <a href="http://plnkr.co/edit/q6Bf9AKLF8OrCEPdtY3Y?p=preview">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload