我的指令中有这个,名为“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
原文由 Raph 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为您需要等待绑定得到解决。例如,通过将构造函数的代码移动到
ngOnInit
钩子中:来自 https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html 的提醒: