在 onInit 函数中访问嵌入槽
Access transclude slot in onInit function
我有一个旧的 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
变量呢?
我找到的解决这个问题的唯一方法:
angular.module('Module').component('myComponent', {
transclude: {
slotA: '?slotA'
},
controller: ['$transclude', ($transclude) => {
this.$onInit = () => {
this.isFilled = transclude.isSlotFilled('slotA');
};
}]
});
我有一个旧的 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
变量呢?
我找到的解决这个问题的唯一方法:
angular.module('Module').component('myComponent', {
transclude: {
slotA: '?slotA'
},
controller: ['$transclude', ($transclude) => {
this.$onInit = () => {
this.isFilled = transclude.isSlotFilled('slotA');
};
}]
});