Angular 反应形式 - 复选框 - 使用现有数组初始化 formArray

Angular reactive form - checkboxes - Initialize formArray with exisitng array

我有一个从服务器获取的 lkBoardTypeList 列表的动态数组。我想在初始化时将该数组推送到形成数组,基本上我想根据数组中的 Board 名称或 ID 显示复选框。我知道如何在反应中推送一个空数组 forms.but 如何使用现有的 array.Actually 及其 update/edit 组件

进行初始化
export class BoardTypeList{
id number;
descr string;
}

组件:

lkBoardTypeList: LkBoardType[] = []; 
SelectedLkBoardTypeList: LkBoardType[] = []; 

this.boardDashboardForm = this.formBuilder.group({
                    id: new FormControl(this.boardObj.id, Validators.required),
                    location: new FormControl(this.boardObj.location),
                    boardType: new FormArray([])
                 });
this.addCheckBoxes();

..//..//

 addCheckBoxes(){
        this.lkBoardTypeList.forEach(() => this.boardTypeFormArray.push(new FormControl(false)));
    }

 get boardTypeFormArray(){ 
        return this.boardDashboardForm.controls.boardType as FormArray;
    }

loadSelected(event){
    
        this.boardService.getBoardTypeById(event.value).subscribe( posts =>{
            this.data = posts; 
            console.log('getBoardTypeById',this.data);
            },
            error => { 
            console.log('getBoardTypeById - error',error);
            this._errorService.handleError(error); 
            }, 
            () => {   
            this.SelectedLkBoardTypeList = this.data.boardTypes;
            for (let i = 0; i < this.lkBoardTypeList.length; i++) {
            ..///.. what code goes here
            }
            }); 
}

并且在 HTML

<div class="row">
                
                <div class="col-sm-2">                
                    <p-dropdown [options]="boardArray" formControlName="id"  (onChange)="loadSelected($event)" placeholder=" Select "></p-dropdown> 
                </div>
                
            </div>
    <div class="form-group" >
                        <div formArrayName="boardType" *ngFor="let c of boardTypeFormArray.controls; let i = index">
                            <input class="col-sm-1" type="checkbox" [formGroupName]="i"/>
                            <span class="col-sm-5" >{{lkBoardTypeList[i].descr}}</span>
                        </div>
                    </div>

现在,当我检查元素时,我看到 formArrayName="boardType" 和复选框元素,如下所示:

<input class="col-sm-1 ng-untouched ng-pristine ng-valid" type="checkbox" ng-reflect-name="0">
<input class="col-sm-1 ng-untouched ng-pristine ng-valid" type="checkbox" ng-reflect-name="1">
<input class="col-sm-1 ng-untouched ng-pristine ng-valid" type="checkbox" ng-reflect-name="2">

我猜 ng-reflect-name="0" 只不过是 'i' 的值。什么正在尝试使用 lkBoardTypeList 中的 id 设置数组的值,即 10,20,30.

另外请告诉我如何设置复选框中选中的值。 SelectedLkBoardTypeList - 例如整个列表的值 (10,20,30)。 selectedList只包含10个,必须勾选

首先,你应该使用[formControlName]="i"而不是[formGroupName]="i"

添加

ng-reflect-${name} 属性用于调试目的,并显示 component/directive 在其 class.

中声明的输入绑定

FormControlName 指令声明

@Input('formControlName') name!: string|number|null;

所以,你是对的:ng-reflect-name="0" 描述了你在 formControlName 输入中传递的值。


如果您想用 SelectedLkBoardTypeList 预填充 FormArray,那么您只需检查 SelectedLkBoardTypeList 中是否存在特定项目。假设您有:

SelectedLkBoardTypeList: LkBoardType[] = [
  {
    id: 10,
    descr: 'Type 1'
  }
];

那么你的addCheckBoxes方法应该如下:

addCheckBoxes() {
  this.lkBoardTypeList.forEach((type) => {
    const selected = this.SelectedLkBoardTypeList.some(x => x.id === type.id);
    this.boardTypeFormArray.push(new FormControl(selected))
  });
}

最后,如果您想在 post 执行期间获取所选项目,您可以将 FormArray 中的所选值映射到原始数组:

save() {
  const model = this.boardDashboardForm.value;

  const selectedTypes = model.boardType.filter(Boolean).map((_, i) => this.lkBoardTypeList[i]);
  console.log(selectedTypes);
}

Ng-run Example