[英]how to send mat-chip-list in form
How to send mat-chip-list inside a form .Even though elements are there still my submit button is disabled.So How Can I send my tag array.please help!如何在表单内发送 mat-chip-list 。即使元素仍然存在,我的提交按钮仍被禁用。所以我如何发送我的标签数组。请帮忙!
below is my code that I've done so far.下面是我到目前为止所做的代码。
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList>
<mat-chip
*ngFor="let tag of Tags"
[selectable]="selectable"
[removable]="removable"
(removed)="remove(tag)">
{{tag}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="tags"
[formControl]="tagsSet"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
</mat-form-field>
removable = true; addOnBlur = false; separatorKeysCodes: number[] = [ENTER, COMMA]; Tags: string[] = []; this.testSuiteForm = new FormGroup({ tagsSet: new FormControl(null, [Validators.required]), }); add(event: MatChipInputEvent): void { const input = event.input; const value = event.value; // Add our tag if ((value || '').trim()) { this.Tags.push(value.trim()); } // Reset the input value if (input) { input.value = ''; } this.tagsSet.setValue(null); } remove(Tags: string): void { const index = this.Tags.indexOf(Tags); if (index >= 0) { this.Tags.splice(index, 1); } } get tagsSet() { return <FormArray>this.testSuiteForm.get('tagsSet'); }
When you call add()
function in ts file you can use setValue for formControl当您在 ts 文件中调用add()
函数时,您可以将 setValue 用于 formControl
Adding JSON based string添加基于 JSON 的字符串
this.testSuiteForm.controls['tagsSet'].setValue(JSON.stringify(this.Tags));
or或者
Adding comma separated string添加逗号分隔的字符串
this.testSuiteForm.controls['tagsSet'].setValue(this.Tags.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.