0

I have a directive that I want to reference in multiple divs. Each div must have access to the same values. Here is a simplified version:

.directive('sampleDir', function() {

  var control = function($scope, $element, $attr) {
    $scope.value1 = '';
    $scope.value2 = '';
  }

  return {
    controller: control,
    link: link // defined in code somewhere and needs access to separate value1 and value2
  };

});

So basically each div that uses this directive should have its separate values for value1 and value2, or should have a separate controller maybe? How would I do this?

2 Answers 2

1

First off, you're using link and controller wrong.

The parameters you provide in control() are the parameters for link(). You need to use dependency injection in control().

Secondly, if you want a instance of the controller per directive, you need to use the scope : true option in your directive's return;

Sign up to request clarification or add additional context in comments.

Comments

0

A separate controller instance (and scope) will get created for each instance of the directive, so you do not have to do anything special here to ensure each instance of the directive gets its own scope.

If you want multiple directive instances which are otherwise unrelated to share state or other data then use a service or factory.

1 Comment

A new instance of the controller will not be created unless you explicitly tell it to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.