2

If two angular directives are attached to the same DOM element do they both share the same scope? The angular documentation implicitly indicates this is the case, If so, what is the best practice for preventing name spacing issues between directives?

E.g.

<div directive-1 directive-2>
</div>

where directive-1 and directive-2 have set scope: true or scope: {...}

Michael Stone
  • 177
  • 1
  • 8

1 Answers1

0

Best practice for these kind of things is the require property in directives, you should be able to require the controller of directive-1 and through that access its scope. But now you also have a shareable API between directives.

app.directive('directive1', function () {
  return {
    require: '^directive1',
    controller: ['$scope', function ($scope) {
      this.$scope = $scope;

      this.sharedFn = function () {

      };

      return this;
    }]
  };
});

app.directive('directive2', function () {
  return {
    require: '^directive1',
    link: function (scope, element, attrs, directive1Ctrl) {
      console.log(directive1Ctrl);
    }
  };
});

app.directive('directive3', function () {
  return {
    require: '^directive1',
    link: function (scope, element, attrs, directive1Ctrl) {
      console.log(directive1Ctrl);
    }
  };
});

Hope this helps

Max
  • 1,526
  • 2
  • 16
  • 19