gpt4 book ai didi

angular - 如何以表格形式发送mat-chip-list

转载 作者:行者123 更新时间:2023-12-04 15:55:06 30 4
gpt4 key购买 nike

如何在表单内发送 mat-chip-list。即使元素仍然存在,我的提交按钮仍被禁用。那么我如何发送我的标签数组。请帮忙!

这是我的表格 form

下面是我到目前为止完成的代码。

<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');
}

最佳答案

当您在 ts 文件中调用 add() 函数时,您可以为 formControl 使用 setValue

添加基于 JSON 的字符串

this.testSuiteForm.controls['tagsSet'].setValue(JSON.stringify(this.Tags));

添加逗号分隔的字符串

this.testSuiteForm.controls['tagsSet'].setValue(this.Tags.toString());

关于angular - 如何以表格形式发送mat-chip-list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52057840/

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