1
app.directive("itemsContainer", function() {

    return {
        restrict: "E",
        controllerAs: "itc",
        bindToController: true,
        controller: function() {
            this.showItems = false;

            this.items = null;

            this.loadItems = (data) => {
                this.items = data;
                this.showItems = true;
            }

            this.hideSummary = () => {
                this.showItems = false;
            }
        },
        templateURL:'itemsContainer.html'
    };

});

app.directive("itemsSummary", function() {

    return {
        restrict: "E",
        require: "^itc",
        link: function(scope, element, attrs, ctrl) {
            scope.showSummary = ctrl.showItems;
            scope.items = ctrl.items;
        },
        templateURL:'itemsSummary.html'
    };
});

app.directive("itemsList", function() {

    return {
        restrict: "E",
        require: "^itc",
        scope: {
            items = "="
        },
        link: function(scope, element, attrs, ctrl) {

          if(items !== null)
          {
              ctrl.loadItems(items);
          }

          scope.hideSummary = () => {
              ctrl.hideSummary();
          }

        },
        templateURL:'itemsList.html'
    };
});

<itemsContainer>
<itemsSummary>{{itemsSummary}}</itemsSummary>
<itemsList>{{items}}</itemsList>
</itemsContainer>

Here, when itemsList directive set the Hide summary using itemsContainer controller, which is not updated in itemsSummary? how to make all the three directive in sync? Best way to communicate between sibling directive? Currently am doing with Event emit which I don't want to do. I need a best practice solution.

My requirement:

<parent> <child1></child1> <child2></child2> </parent>

How to communicate any update in child2 to child1?

1 Answers1

0

You need to do manual transclution, that's what I did in a similar situation. Anglers default transclution won't work, since it creates a new scope.

<itemsContainer>
  <itemsSummarydata="$ctrl.data"></itemsSummary>
</itemsContainer>


/** * Parent component */

angular.module("app").component("itemsContainer", function() {
    constructor() {
        return {
            restrict: 'E',
            replace: true,
            template: "<div transclude-extended></div>",
            transclude: true,
            bindToController: true,
            controller: function () {
                var ctrl = this;
                    ctrl.data = 'This is sample data.';
            }
        };
    }
});

/** * Transclude Extended Directive */

angular.module("app").directive("transcludeExtended", function(){
    constructor() {
        return {
            link: function ($scope, $element, $attrs, controller, $transclude) {
                var innerScope = $scope.$new();
                $transclude(innerScope, function (clone) {
                    $element.empty();
                    $element.append(clone);
                    $element.on('$destroy', function () {
                        innerScope.$destroy();
                    });
                });
            }
        }
    };
});

transcludeExtended is the manual way of doing the translation, instead of ng-transclude

Reference: https://github.com/angular/angular.js/issues/1809