角度指令-ElementRef-无法从mat-form-field中选择mat-select

罗布斯

我正在尝试通过指令访问的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-select在控制台中看到了在此处输入图片说明

我在这里想念什么??

这是该指令的应用方式:

<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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

mat-form-field必须包含MatFormFieldControl

在属性指令中使用ElementRef?

角度2-在elementRef上设置选择

无法使用mat-tab或mat-ink-bar(角度)自定义CSS

角度材质图标mat-icon无法正确显示

量角器从下拉列表中选择值,使用mat-select实现

为什么在此FormArray中无法编辑mat-form-field?

无法从mat-form-field获取选定的选项以在angular2中动态生成url

<mat-form-field>的错误。正常输入工作正常,但不能使用mat-select而不会出现错误

解决错误后,mat-form-field不会删除mat-form-field-invalid类

我可以将mat-form-field外包为自定义组件或指令吗?

角度ngx-mat-select-search自定义组件

与mat-form-field中的matSuffix mat-checkbox交互

如何基于值禁用角度的mat-form-field?

一旦在Angular 8中选择了Mat-Table,就禁用Mat-Select选项

如何从Mat-form-field中删除Mat-form-field-wrapper?

在mat-select中选择一个值会导致尝试“ diff”时出错

角度ng-content不适用于mat-form-field

为什么不能选择mat-select的mat-option?

在自定义指令内使用ViewChild / ContentChild在mat-form-field内找到输入[matInput]

从模板中选择 mat-select 的初始值

mat-select selectionChange 获取组角度

Angular4 mat-form-field textArea 无法启用水平调整大小

mat-list-item 重叠中的 mat-form-field

Angular:mat-select,reactive form,disable 不显示虚线

如何检查 ElementRef 是否包含角度打字稿中的 mat-icon 节点?

无法使用 css 选择器修改角度材料表单字段类“mat-form-field-outline”

如何获得 Mat Autocomplete 的 Panel ElementRef

如何调整mat-form-field mat-chip的大小?