Доступ к слоту transclude в функции onInit - PullRequest
0 голосов
/ 23 мая 2018

У меня есть старый компонент AngularJS <1.4, который я хочу преобразовать, используя <code>angular.component('component', {bindings..., onInit...}).Предыдущий код выглядит так:

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

, который я хотел бы преобразовать во что-то вроде:

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

, но как тогда получить доступ к переменной transclude?

1 Ответ

0 голосов
/ 23 июня 2018

Единственный способ, который я нашел, чтобы решить эту проблему:

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