我的指令名为“ 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] 删除。
我来说两句