在onInit函数中访问transclude槽

时间:2018-05-23 10:44:33

标签: angularjs angularjs-ng-transclude transclusion

我有一个旧的AngularJS< 1.4我要使用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 :(得分:0)

我找到解决此问题的唯一方法:

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