0

I'm trying to create a service for two sibling directives to speak through.

I've got a from and a to date-time-picker directive, both of which share the same template and gets set from the template of shiftsController, based on a few attributes like name and a combination of yesterday's, today's and tomorrow's date, depending on which of the two siblings are created.

As mentioned above they are both initiated on shifts.html, within a shiftsController as vm <div>:

<div>
    <date-picker ctrl="from"></date-picker>
</div>
<div>
    <date-picker ctrl="to"></date-picker>
</div>

However I seem to be getting an error when attempting to require the shiftsController in the directive:

angular.module("datePicker", [])
    .directive("datePicker", function () {

        function link(scope, element, attrs, controller) {
            controller.doSomething();
        }
        return {
            restrict: "E",
            scope: {
                ctrl: '='
            },
            link: link,
            templateUrl: "app/views/datepicker.html",
            require: '^shiftsController'
        };
    })

I was under the impression (from multiple articles) that this would be the way to go about setting up the communications between sibling directives.

geostocker
  • 1,190
  • 2
  • 17
  • 29
  • Have you imported shiftsController? – rrd May 22 '17 at 13:59
  • Actually, a directive's `require` property is meant to reference another _directive_, whose controller will be then injected in the `link` function. See this [article](https://toddmotto.com/directive-to-directive-communication-with-require/), I find it quite complete. – SinDeus May 22 '17 at 14:12
  • @rrd wouldn't the shiftsController already be 'imported' due to it being the parent? At least according to other articles that's how it *should* work :P – geostocker May 22 '17 at 14:16
  • https://stackoverflow.com/questions/15672709/how-to-require-a-controller-in-an-angularjs-directive – Manikandan Velayutham May 23 '17 at 05:51

0 Answers0