我正在尝试通过指令访问的mat-select
内部mat-form-field
。
@Directive({ selector: "[appMatSelectLoader]" })
export class MatSelectLoaderDirective {
constructor(el: ElementRef) {
console.log((el.nativeElement as HTMLElement).querySelector("mat-select"));
}
}
我进入null
控制台。但是,当我这样做时:
console.log((el.nativeElement as HTMLElement));
我在这里想念什么??
这是该指令的应用方式:
<mat-form-field appearance="fill" appMatSelectLoader>
<mat-select required>
<mat-option *ngFor="let option of options" [value]="option.id">
{{ option.name }}
</mat-option>
</mat-select>
</mat-form-field>
您正在尝试在视图初始化之前查询dom
export class MatSelectLoaderDirective implements AfterViewInit {
el: ElementRef;
constructor(el: ElementRef) {
this.el = el;
}
ngAfterViewInit () {
console.log((this.el.nativeElement as HTMLElement).querySelector("mat-select"));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句