Access transclude slot in onInit function

113 Views Asked by At

I have an old AngularJS < 1.4 component that I want to convert using angular.component('component', {bindings..., onInit...}). The previous code looks like this:

angular.module('Module').directive('myComponent', [
  () => {
    return {
      restrict: 'E',
      transclude: {
        slotA: '?slotA'
      },
      link(scope, _element, _attrs, _ctrl, transclude) {
        scope.isFilled = transclude.isSlotFilled('slotA');
      }
    };
  }
]);

that I would like to convert to something like:

angular.module('Module').component('myComponent', {
  transclude: {
    slotA: '?slotA'
  },
  onInit() {
    this.isFilled = transclude.isSlotFilled('slotA');
  }
});

but how to access the transclude variable then?

1

There are 1 best solutions below

0
Guillaume On BEST ANSWER

The only way I found to solve this:

angular.module('Module').component('myComponent', {
  transclude: {
    slotA: '?slotA'
  },
  controller: ['$transclude', ($transclude) => {
    this.$onInit = () => {
      this.isFilled = transclude.isSlotFilled('slotA');
    };
  }]
});