ViewChild无法从按钮中提取指令

<button alloy #initialFocus></button>

通过以下方式访问:


    ngOnInit(): void {
      this.initalFocusButton.focus();  // undefined unless it's an ElementRef
    }

在OnInit和AfterViewInit(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.
    ...
tangjunmei 回答:ViewChild无法从按钮中提取指令

可能没有将指令作为指令使用-因此它只是另一个HTML属性。向其添加默认构造函数,并检查它是否被调用。

本文链接:https://www.f2er.com/3122470.html

大家都在问