【发布时间】:2020-03-07 09:45:55
【问题描述】:
<button alloy #initialFocus></button>
通过以下方式访问时:
ngOnInit(): void {
this.initalFocusButton.focus(); // undefined unless it's an ElementRef
}
在 OnInit 和 AfterViewInit 中出现 undefined(ng8 所以没关系)。我尝试了read 和选择器的各种组合,但它只能作为ElementRef 进行查询。在原生元素上查询指令是否有限制?
AlloyButtonDirective:
@Directive({
selector: `button [alloy]`
})
export class AlloyButtonDirective implements AfterViewInit, OnDestroy {
private unsubscribe = new Subject();
// Default mode: Standard
private currentStyle = Styles.Standard;
@HostBinding(`class.alloy-button-standard`) @Input('standard')
get isStandard() { return this.currentStyle === Styles.Standard; }
set isStandard(value: any) {
if (value !== false) {
this.currentStyle = Styles.Standard;
}
}
// More styles and a focus monitor.
...
【问题讨论】:
标签: angular directive viewchild