gpt4 book ai didi

angular - 单击按钮时, Angular 4 中的 react 形式提交复选框值数组

转载 作者:行者123 更新时间:2023-12-04 17:42:14 25 4
gpt4 key购买 nike

我正在使用 Angular 4 开发一个应用程序。我正在使用 Reactive 表单。我正在构建一个简单的提交表单,如下所示:

<form [formGroup]="newPostForm" novalidate>

<input formControlName="Title" type="text" class="form-control" id="txtTitle" placeholder="Enter title of post...">
<h4>Categories</h4>
<ul>
<li *ngFor="let cat of catsList">
<span style="margin-top: -2.5px;">
<input id="CheckBox" type="checkbox" formNameArray="Categories" [value]='cat.CategoryID' checked='false'></span> {{cat.CategoryName}}
</li>
</ul>
<input type="submit" name="btnSubmit" value="Save" id="btnSubmit" (click)="onSubmitedForm()" class="btn btn-primary">
</form>

下面是 new-post.component.ts 文件:

export class NewPostComponent implements OnInit {
public catsList: any = [];
public newPostDTO: any = [];
newPostForm: FormGroup;

constructor(private _postsSvc: PostsService,
private _categoriesSvc: CategoriesService,
private _formGroup: FormBuilder) { }

ngOnInit() {
this.getAllCategories();
this.initProperties(); //init all props
this.createPostForm();
}


initProperties() {
this.newPostDTO = {
Title: null,
Content: null,
Categories: null
};
}

createPostForm() {
this.newPostForm = this._formGroup.group({
Title: [''],
Categories: new FormArray([])
});
}

onSubmitedForm() {
console.log(this.newPostForm.value.Categories);
this.newPostDTO = {
Title: this.newPostForm.value.Title,
Categories: this.newPostForm.value.Categories,
};
this._postsSvc.addPost(this.newPostDTO).subscribe(data => {
if (data.status === 201) {

}
});
}


getAllCategories() {
this._categoriesSvc.getCategories().subscribe(
data => this.catsList = data.body );
}

}单击提交按钮时,我想将所有选中类别的 ID 作为数组获取。我怎样才能做到这一点?我试过了,但在我点击提交按钮后,类别数组的值为空。

谢谢大家!

最佳答案

响应式表单控件源是类而不是模板,所以如果你想创建动态复选框你需要创建多个表单控件

如果您希望更改仅在提交后发生,您可以传递 {updateOn:"submit"} 以仅在提交后获取数据

catsList = [{cName:"cat1"},{cName:"cat2"},{cName:"cat3"}];

newPostForm: FormGroup;
constructor(private fb: FormBuilder) {
const control = this.catsList.map((d)=>new FormControl())
this.newPostForm = this.fb.group({
Title: [''],
Categories: new FormArray(control,{updateOn:"submit"})
})
}

get catControl() {
return this.newPostForm.get('Categories')['controls'];
}

onSubmitedForm() {

}

FormArray 是基于索引的,所以我们应该为单独的 formControl 使用索引

<form [formGroup]="newPostForm" > 
<input formControlName="Title" type="text" class="form-control" id="txtTitle" placeholder="Enter title of post...">
<h4>Categories</h4>
<ul>
<li *ngFor="let cat of catControl;let i =index">
<span style="margin-top: -2.5px;" formArrayName="Categories">
<input [formControlName]="i" id="CheckBox" type="checkbox" ></span> {{catsList[i].cName}}
</li>
</ul>
<input type="submit" name="btnSubmit" value="Save" id="btnSubmit" (click)="onSubmitedForm()" class="btn btn-primary">
</form>

示例:https://stackblitz.com/edit/angular-dzxcf1

关于angular - 单击按钮时, Angular 4 中的 react 形式提交复选框值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53938284/

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