<p [attr.attribute-you-are-targeting]="expression"></p>
When the expression resolves to
null
orundefined
, Angular removes the attribute altogether.
方法1 - html:
<input type="text" formControlName="name" [attr.disabled]="readOnly || null">
方法2 - formGroup:
<input type="text" formControlName="name">
if (this.readOnly) {
this.formGroup.get('name').disable();
}