Angular2 ElementRef nativeElement检查元素是否被禁用

拉夫

我的指令名为“ bgcolor”,其中包含以下内容:

export class BgColorDirective {
    constructor(el: ElementRef) {
        console.log(el.nativeElement.disabled); // show "false"
        if (el.nativeElement.disabled) {
            el.nativeElement.style.backgroundColor = '#5789D8';
            el.nativeElement.style.color = '#FFFFFF';
        }
    }

在我的模板中,我有:

<button (click)="submitData()" [disabled]="true" bgcolor [routerLink]="['Onboarding']"> </button>

我不明白为什么会el.nativeElement.disabled回来false

蒂埃里圣堂武士

我认为您需要等待绑定解决。例如,通过将构造函数的代码移到ngOnInit钩子中:

export class BgColorDirective {
  constructor(private el: ElementRef) {
  }

  ngOnInit() {
    console.log(this.el.nativeElement.disabled); // show "true"
    if (this.el.nativeElement.disabled) {
        this.el.nativeElement.style.backgroundColor = '#5789D8';
        this.el.nativeElement.style.color = '#FFFFFF';
    }
  }
}

提醒您https : //angular.io/docs/ts/latest/guide/lifecycle-hooks.html

ngOnInit:在Angular初始化数据绑定的输入属性之后,初始化指令/组件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Angular 2中获取与ElementRef关联的组件的参考

Angular2在单元测试中注入ElementRef

获取根组件ElementRef或ComponentRef angular 2

Angular2:ElementRef nativeElement与querySelector性能

Renderer和ElementRef在角度2中的区别

Angular2:获取DOM-Element的尺寸(无ElementRef)

angular2:ElementRef获取CSS填充属性

Angular2 @ViewChild ElementRef offsetHeight始终为0

Angular2 getElementsByClassName在ElementRef上

替代elementRef.nativeElement.id

角度2-在elementRef上设置选择

Angular 4:模拟ElementRef

Angular 2 +如何使用同一选择器(elementRef.nativeElement)选择和循环多个元素

Angular获取routerLinkActive的ElementRef

Angular:如何处理空的ElementRef?

从Angular中的Template引用变量获取ElementRef

如何解决使用ViewChild ElementRef时无法读取angular中未定义的属性'nativeElement'

为什么elementRef的nativeElement未定义?

使用硒检查元素是否被禁用

Angular2:禁用formbuilder的元素

Angular 2使用elementref获取多个元素

ElementRef.find()不是角度函数2

在 Angular 的点击事件中访问 elementRef

如何在 angular 2 中使用 ELementRef 或 @ViewChild 访问特定的 DOM 元素?

在 ElementRef/本机元素中使用角度函数

如何使用 Renderer2 将样式应用到 ElementRef -> nativeElement 在 Angular 的子节点

Angular7,获取 ElementRef

Angular 12 ViewChild ElementRef

我可以强制 HTML 元素的 Angular #variableName 返回 ElementRef 吗?