【发布时间】:2020-09-23 17:45:07
【问题描述】:
当用户使用 setValue() 为这些字段动态设置表单值时,如何仅将少数字段(名字和电子邮件)的 Angular 反应式表单字段文本颜色更改为“红色”。还需要将表单字段(名字和电子邮件)的颜色更改为“绿色”,以防用户尝试修改/交互。
但不应更改其他表单字段的颜色。
我在以下链接中创建了 stackblitz
reactive form updated code in stackblitz
我已经创建了这样的表单,
<form name="form" [formGroup]="form">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" formControlName="firstName" [style.color]="getColor('firstName')" (input)="inputChanged($event)"/>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" formControlName="email" [style.color]="getColor('email')" (input)="inputChanged($event)" />
</div>
<div class="form-group">
<label for="phone">phone</label>
<input type="text" class="form-control" formControlName="phone" [style.color]="getColor('phone')" (input)="inputChanged($event)" />
</div>
<button class="btn btn-primary" (click)="setFormm()">click here</button>
</form>
组件
@Component({
selector: "app-formreactive",
templateUrl: "./formreactive.component.html",
styleUrls: ["./formreactive.component.css"]
})
export class FormreactiveComponent {
form = new FormGroup({
firstName: new FormControl(""),
email: new FormControl("")
});
getColor(frmctrl, color) {
}
inputChanged = (e: any) => {
};
setFormm = () => {
this.form.controls['firstName'].setValue('vishnu');
this.form.controls["email"].setValue("vishnu@hminds.com");
};
【问题讨论】:
标签: angular angular-reactive-forms