简体   繁体   English

如何使用Angular5在反应性formGroup中禁用formArray的表单元素

[英]how to disable a form element of formArray in reactive formGroup using Angular5

I am having two dropdown in an formArray. 我在formArray中有两个下拉菜单。

<select formControlName="access" (change)="checkValue($event)" >
  <option[value]="1">Admin</option>
  <option[value]="2">Customer</option>
</select>

<mat-select  formControlName="regions" multiple>
  <mat-option *ngFor="let region of regionArray" [value]="region.regionId">{{region.description}}</mat-option>
</mat-select>

I am disabling the regions selection if the access selected is as Admin. 如果所选访问权限为“管理员”,则我将禁用区域选择。

checkValue(event) {  
    if (event.currentTarget.value === "1") {
        return this.myForm = this.fb.group({
                user: this.fb.array([this.disableDetail(),   ])
            }); 
    } else {  
        return this.myForm = this.fb.group({
                user: this.fb.array([this.enableDetail(),    ])
            }); 
    } 
}

enableDetail() {    
    return this.fb.group({
        regions: [{value:'',Validators: Validators.required, disabled:false}]     });   
    }

disableDetail() {
    return this.fb.group({
        regions: [{value:'', disabled:true}]    });  
}

I am facing issue that it is affecting the other form array. 我面临的问题是它会影响其他表单数组。 I want if the dropdown value I am choosing as Admin it should disable and select all values of region dropdown only for that particular formarray. 我想要如果我选择作为管理员的下拉列表值应该禁用并选择仅针对该特定formarray的区域下拉列表的所有值。

HTML 的HTML

<select formControlName="access" (change)="checkValue($event)" >
  <option[value]="1">Admin</option>
  <option[value]="2">Customer</option>
</select>

<mat-select [disabled]="isAdmin" formControlName="regions" multiple>
  <mat-option *ngFor="let region of regionArray" [value]="region.regionId">{{region.description}}</mat-option>
</mat-select>

Component 零件

isAdmin: boolean;
checkValue(event) {  
    this.isAdmin = event.currentTarget.value === "1";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular反应形式的FormGroup中的FormArray上进行迭代 - How to iterate over FormArray in FormGroup in an Angular reactive Form Angular 9 反应形式; 带有嵌套 FormArray 的 FormGroup 的 PatchValue - Angular 9 Reactive Form; PatchValue of FormGroup with nested FormArray 角度反应形式在另一个 FormGroup 内的另一个 FormArray 内的嵌套 FormGroup 内添加 FormArray - angular reactive form add FormArray inside a nested FormGroup inside another FormArray inside another FormGroup Angular 反应式 Forms FormArray - 无法分配第二个 FormGroup 元素 - Angular Reactive Forms FormArray - cannot assign second FormGroup element Angular 2-使用FormBuilder,FormGroup,FormArray构建带有无线电输入的动态反应形式 - Angular 2 - Dynamic Reactive form with Radio Inputs build with FormBuilder, FormGroup, FormArray 如何使用Angular6仅使用FormArray创建反应形式? - How to create a Reactive Form using only FormArray using Angular6? Angular 2 Reactive Forms中FormArray内的FormGroup - FormGroup inside FormArray in Angular 2 Reactive Forms 嵌套FormGroup中具有FormArray的Angular反应形式 - Angular Reactive Forms with FormArray inside a nested FormGroup 如何在 FormGroup 中使用 FormArray - How to Using a FormArray in a FormGroup 在 Angular 中推送 FormArray 内的 FormGroup 反应式 Forms - Push FormGroup inside FormArray in Angular Reactive Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM