Using current controller in directive with inheritence

218 Views Asked by At

I have 3 directives which require themselves this way:

    .directive('grandParent', function () {
      return {
        retrict: 'E',
        controller: function ($scope, $element, $attrs) {
          this.foo = function () {

          };
        },
        link: function(scope, element, attrs, ctrl) {
          // ...
        }
      }
    })

    .directive('parent', function () {
      return {
        retrict: 'E',
        require: ['parent', '^grandParent'],
        controller: function ($scope, $element, $attrs) {
          this.foo = function () {

          };
        },
        link: function(scope, element, attrs, ctrls) {
          var parentCtrl = ctrls[0],
            grandParentCtrl = ctrls[1];
          // ...
        }
      }
    })

    .directive('child', function () {
      return {
        retrict: 'E',
        require: ['child', '^parent', '^grandParent'],
        controller: function ($scope, $element, $attrs) {
          this.foo = function () {

          };
        },
        link: function(scope, element, attrs, ctrls) {
          var childCtrl = ctrls[0],
              parentCtrl = ctrls[1],
              grandParentCtrl = ctrls[2];
          // ...
        }
      }
    })

I need in the child to use parent and grand parent controller functions, that why I didn't populate their link function with the business code.

I didn't find anywhere this kind of call of current directive controller.

I'm wondering if there is any problem with this kind of practice (circular reference...) or if it's a bad practice and there are other way to do that.

0

There are 0 best solutions below