gpt4 book ai didi

javascript - 如何在Angular Reactive表单中以动态方式根据条件显示/隐藏字段

转载 作者:行者123 更新时间:2023-12-05 00:31:13 24 4
gpt4 key购买 nike

这里我的场景是我有 3 个用户
1.管理员将有 3 个字段电子邮件,名字,姓氏。
2. 员工将有 4 个字段电子邮件,名字,姓氏,联系方式。
3.前台将有 5 个字段电子邮件、名字、姓氏、航空公司详细信息、 vendor 、人名。
stackblitz 链接 -: https://stackblitz.com/edit/angular-material-forms-deborahk-jgxzic
根据条件,我必须显示这些字段并根据此处为这些字段设置值,我遵循了一种方法,我正在禁用如下所示的字段

 this.userForm = new FormGroup({
email : new FormControl(null, Validators.email),
firstName : new FormControl(null, Validators.required),
lastName : new FormControl(null, Validators.required),
contact: new FormControl({value: '', disabled: false}, Validators.required),
airlineDetails: new FormControl({value: '', disabled: false}, Validators.required),
vendor: new FormControl({value: '', disabled: false}, Validators.required),
personNames: new FormControl({value: '', disabled: false}, Validators.required)
});

if(this.userOne=="admin"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="employee"){
this.userForm.get('contact').enable();
this.userForm.get('airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="frontOffice"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').enable();
this.userForm.get('vendor').enable();
this.userForm.get('personNames').enable();
}
}
有没有其他方法可以隐藏和显示这些字段,然后在其中设置值
下面是我的代码:
<mat-toolbar color="primary">
<button type="button" (click)=" userData('admin')">ADMIN</button>
&nbsp;
<button type="button" (click)=" userData('employee')">EMPLOYEES</button>
&nbsp;
<button type="button" (click)=" userData('frontOffice')">frontOffice</button>

</mat-toolbar>

<div class="container" >
<form [formGroup]=" userForm" (ngSubmit)="onClick()" class="form">
<!--Email-->
<mat-form-field class="form-element" (ngSubmit)="onClick()">
<input matInput type="email" placeholder="Email Address" formControlName="email">
</mat-form-field>
<!--First Name-->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="First name" formControlName="firstName">
</mat-form-field>
<!--last Name-->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="First name" formControlName="lastName">
</mat-form-field>

<!------------------------------------------------------------------->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="contact" formControlName="contact">
</mat-form-field>
<!------------------------------------------------------------------->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="airline details" formControlName="airlineDetails">
</mat-form-field>
<!------------------------------------------------------------------->
<mat-form-field class="form-element">
<input matInput type="text" placeholder="vendor" formControlName="vendor">
</mat-form-field>

<mat-form-field class="form-element">
<input matInput type="text" placeholder="persons Names" formControlName="personNames">
</mat-form-field>

<button type="submit" [disabled]="userForm.invalid">Submit</button>


</form>
</div>
.ts 代码
 userForm: FormGroup;
titleAlert: string = 'This field is required';
post: any = '';
userOne="admin";
userTwo="employee";
userThree="frontOffice"

constructor(private formBuilder: FormBuilder) { }

ngOnInit() {
this.createForm();

}

createForm() {
this.userForm = new FormGroup({
email : new FormControl(null, Validators.email),
firstName : new FormControl(null, Validators.required),
lastName : new FormControl(null, Validators.required),
contact: new FormControl({value: '', disabled: false}, Validators.required),
airlineDetails: new FormControl({value: '', disabled: false}, Validators.required),
vendor: new FormControl({value: '', disabled: false}, Validators.required),
personNames: new FormControl({value: '', disabled: false}, Validators.required)
});

}



onClick(){
console.log(this.userForm.value)
}

userData(params){
if(this.userOne=="admin"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="employee"){
this.userForm.get('contact').enable();
this.userForm.get('airlineDetails').disable();
this.userForm.get('vendor').disable();
this.userForm.get('personNames').disable();
}
if(this.userTwo=="frontOffice"){
this.userForm.get('contact').disable();
this.userForm.get(' airlineDetails').enable();
this.userForm.get('vendor').enable();
this.userForm.get('personNames').enable();
}
}

最佳答案

因此,您实际上可以在禁用/隐藏的表单元素上设置值。如果您想在您引用/传递给后端的 formData 中包含禁用字段,那么您用来引用表单元素的对象需要是:

this.userForm.getRawValue();
这将获取所有表单属性,而 form.value 属性不包括禁用元素。您可以使用组件文件设置表单值
this.userForm.get('contact').setValue(<my-value>)
但是为了安全措施,我随时更新表单元素值我运行该功能
this.userForm.get('contact').updateValueAndValidity();
// I think this.userForm.updateValueAndValidity() may work on the whole form,
// but last time I was messing with it I had some issues.
编辑
为了隐藏在模板中而不拥挤我会做的dom
<ng-container *ngIf="userForm.get('contact').disabled">...</ng-container>

关于javascript - 如何在Angular Reactive表单中以动态方式根据条件显示/隐藏字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62998505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com