angularjsangularjs-ng-transcludetransclusion

Access transclude slot in onInit function


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?


Solution

  • 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');
        };
      }]
    });