Angular 如何导出结构指令属性以在模板中使用?

Angular 如何导出结构指令属性以在模板中使用?,angular,angular-directive,Angular,Angular Directive,我有一个结构指令: @指令({ //tslint:禁用下一行:指令选择器 选择器:“[循环]” }) 导出类循环直接{ 私人周期$:任意; 建造师( 私人tRef:TemplateRef, 私有vcRef:ViewContainerRef){} @Input()cycleInterval:number |可观测=1000; @Input()设置周期(源:数组){ const interval$=类型(数字,this.cycleInterval)? 可观察计时器(0,this.cycleInter

我有一个结构指令:

@指令({
//tslint:禁用下一行:指令选择器
选择器:“[循环]”
})
导出类循环直接{
私人周期$:任意;
建造师(
私人tRef:TemplateRef,
私有vcRef:ViewContainerRef){}
@Input()cycleInterval:number |可观测=1000;
@Input()设置周期(源:数组){
const interval$=类型(数字,this.cycleInterval)?
可观察计时器(0,this.cycleInterval作为数字):this.cycleInterval;
if(!hasProperty(interval$,'subscribe')){
抛出新错误('输入间隔必须为数字或可观察');
}
此.cycle$=可观测的(源)
.CombineTest(可观测区间$)
.map(([items,index])=>items[cycle(index-1,items.length)])
.forEach(项目=>{
这个.vcRef.clear();
this.vcreef.createEmbeddedView(this.tRef);
});
}
}
如何导出当前项、索引(或任何内容),以便能够在模板中使用它

<div *cycle="['a', 'b', 'c']; interval: 2000">
  testing: <!-- current item -->
</div>
您可以尝试:

this.vcreef.createEmbeddedView(this.tRef,{$implicit:item,index:index});
在模板中:

*cycle=“['a','b','c'];间隔:2000;let item;let i=索引”
根据您的要求:

指令.ts

@指令({
//tslint:禁用下一行:指令选择器
选择器:'[cycle][cycleOf]'
^^^^^^但我们可以忽略这一点
})
导出类循环直接{
私人周期$:任意;
建造师(
私有tRef:TemplateRef
工作正常(:
<div *cycle="let item of ['a', 'b', 'c']; interval: 2000">{{ item }}</div>