我正在使用这个来验证我的表单:
this.form = this.fb.group({
UnitName: [this.editUnit.UnitName, Validators.required],
Area: [this.editUnit.Area, Validators.required],
OwnerId: [this.editUnit.OwnerId],
IsWithParking: [this.editUnit.IsWithParking],
ParkingArea: [this.editUnit.ParkingArea],
});
这是以html形式实现的
<mat-card style="margin-bottom: 10px;">
<mat-card-content>
<form [formGroup]="form">
<div class="col-md-12">
<mat-form-field>
<input type="text" matInput formControlName="UnitName" placeholder="Unit Name">
<mat-error *ngIf="UnitName.invalid" style="color: red;">{{unitNameError()}}</mat-error>
</mat-form-field>
<mat-form-field>
<input type="number" matInput formControlName="Area" placeholder="Unit Area">
<mat-error *ngIf="UnitName.invalid" style="color: red;">{{areaError()}}</mat-error>
</mat-form-field>
</div>
<div class="col-md-12">
<mat-form-field>
<mat-select placeholder="Unit Owner" formControlName="OwnerId">
<mat-option *ngFor="let p of persons" [value]="p.PersonId">
{{p.FirstName}} {{p.MiddleName}} {{p.LastName}}
</mat-option>
</mat-select>
<mat-error *ngIf="OwnerId.invalid" style="color: red;">{{ownerIdError()}}</mat-error>
</mat-form-field>
</div>
<div class="col-md-12">
<mat-form-field>
<mat-select formControlName="IsWithParking" placeholder="With Parking">
<mat-option *ngFor="let y of yesNo" [value]="y.value">
{{y.name}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<div class="col-md-12">
<mat-form-field>
<input type="number" matInput placeholder="Parking Area Size" formControlName="ParkingArea">
</mat-form-field>
</div>
<button mat-raised-button class="btn btn-primary" (click)="updateUnit()" [disabled]="form.invalid">
Update Unit
</button>
<button mat-raised-button class="btn btn-warning" (click)="cancel()">
Cancel
</button>
</form>
</mat-card-content>
</mat-card>
如您在图像中所见,在未执行任何操作的情况下,必填输入字段的线条和字体具有默认颜色,但对于那些没有必填属性的输入字段,其线条颜色和字体为绿色。一旦我开始填写必填的输入字段,我就会得到绿色的线条和字体颜色。有没有办法改变这种绿色?它真的不符合我的红色主题颜色。非常感谢。
最佳答案
由于您使用的是 Angular Material要呈现表单,您可能需要查看 defining a custom Angular Material theme (或 re-theming components on an individual basis )。
关于css - 更改 Angular Material 中的验证颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462537/