angular-如何获取没有exportAs的指令的引用
clrDate是没有exportAs语句的自定义第三方指令 源代码angular-如何获取没有exportAs的指令的引用,angular,angular-directive,vmware-clarity,Angular,Angular Directive,Vmware Clarity,clrDate是没有exportAs语句的自定义第三方指令 源代码 @Directive({ selector: '[clrDate]', host: { '[class.clr-input]': 'true', }, providers: [DatepickerFocusService], }) export class ClrDateInput extends WrappedFormControl<ClrDateContainer> implements
@Directive({
selector: '[clrDate]',
host: {
'[class.clr-input]': 'true',
},
providers: [DatepickerFocusService],
})
export class ClrDateInput extends WrappedFormControl<ClrDateContainer> implements OnInit, AfterViewInit, OnDestroy {
@Input() placeholder: string;
@Output('clrDateChange') dateChange: EventEmitter<Date> = new EventEmitter<Date>(false);
@Input('clrDate')
...
}
我希望能够从控制器内部以及customDirective中获取对它的引用。我该怎么做
<clr-date-container customDirective>
<label for="dateControl">Requirement Date</label>
<input id="dateControl" type="date" [placeholder]="'TT.MM.YYYY'" [(clrDate)]="item.requirementDate" (clrDateChange)="onChange($event)" [ngModel]="null" name="requirementDate"/>
<clr-control-error>{{ 'FORMS.VALIDATION.DATE_INVALID' | translate }}</clr-control-error>
</clr-date-container>
在指令上添加ref-template变量,您可以在组件中获得它根据您希望从何处访问指令实例,您应该能够通过@ViewChild或构造函数注入获得它 通过控制器组件或customDirective,您应该能够执行以下操作: //将在ngOnInit提供 @ViewChildClrDateInput,{static:true} clrDateDirective:ClrDateInput; 假设您可以有多个ClrDateInput指令,则可以使用@ViewChildren: //将在ngAfterViewInit中提供 @ViewChildrenClrDateInput clrdatedirections:QueryList; 从指令本身的宿主元素,您可以通过构造函数注入来注入指令: //app.component.html //自定义-app.component.ts @组成部分{ 选择器:自定义应用程序, 模板:` 自定义应用程序 ` } 类CustomAppComponent{ constructor@Self专用ClrDateInput:ClrDateInput{}
您可以看一个例子。这样@ViewChild就可以使用它们的类从组件的模板中选择任何指令了吗?您对如何从位于父元素上的指令访问指令有什么建议吗?我认为您应该能够在不使用@Self decorator的情况下插入它。您是指从我的cu中的本地引用stomDirective?但我想通过另一种方式获得参考。