【发布时间】:2020-10-28 14:36:14
【问题描述】:
我有一个表单中的输入字段,候选人必须输入他的技能。所以我做了一个芯片输入字段。问题是当我重置表格时,芯片中的值被清除但芯片视图仍然存在。我已经按照example 关注MatChips,但没有任何效果。以下是我的代码:
TS
@ViewChild('chipList') chipList: MatChipList;
@ViewChild('tags') tags: ElementRef;
profileForm = this.fb.group({tag: this.fb.array([]),
Recaptcha: [false, Validators.requiredTrue]
});
add(event: MatChipInputEvent, form: FormGroup): void {
const input = event.input;
const value = event.value;
// Add name
if ((value || '').trim()) {
const control = <FormArray>form.get('tag');
control.push(this.initName(value.trim()));
}
// Reset the input value
if (input) {
input.value = '';
}
}
remove(form, index) {
form.get('tag').removeAt(index);
}
get Tags() {
return this.profileForm.get('tag');
}
get ReCaptcha() {
return this.profileForm.get('Recaptcha');
}
registerUser(formDirective: FormGroupDirective) {
for(let i of this.Tags.value) {
this.skills += i + ',';
}
this.skills = this.skills.substring(0, this.skills.length-1);
this.profileForm.patchValue({
skills: this.skills,
mobileNumber: this.MobileNumber.value.replace(/\-/gi, '')
});
if(this.ReCaptcha.value == true) {
this.appService.userRegistration(this.profileForm.value).subscribe(response => {
if(response.message != "Success.") {
this.snackbarService.class = 'red-snackbar';
return this.snackbarService.open('User Registration Unsuccessfull');
}
this.router.navigate(['/login']);
this.snackbarService.class = 'green-snackbar';
this.snackbarService.open('User Registered Successfully');
}, error => {
this.snackbarService.class = 'red-snackbar';
this.snackbarService.open('Something went wrong');
});
formDirective.resetForm();
this.profileForm.reset();
this.tags.nativeElement.value = '';
}
}
HTML
<div class="row">
<div class="col-md-12">
<mat-form-field class="example-chip-list" appearance="outline" style="width: 100%;">
<mat-label>Skills</mat-label>
<mat-chip-list #chipList formArrayName="tag" matTooltip="Press Enter to seperate skills">
<mat-chip *ngFor="let name of profileForm.get('tag')['controls']; let i=index;"
[selectable]="selectable" [removable]="true" (removed)="remove(profileForm, i)">
{{name.value}}
<mat-icon matChipRemove *ngIf="true">cancel</mat-icon>
</mat-chip>
<input #tags [matChipInputFor]="chipList" [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur" (matChipInputTokenEnd)="add($event, profileForm)">
</mat-chip-list>
</mat-form-field>
</div>
</div>
<recaptcha formControlName="Recaptcha" (scriptError)="onScriptError()"></recaptcha>
<button type="submit" class="float-right" [disabled]="profileForm.invalid || !ReCaptcha"
class="btnRegister btn">Register</button>
app.module
import { RecaptchaModule } from 'angular-google-recaptcha';
@NgModule({
imports: [
RecaptchaModule.forRoot({
siteKey: 'SITE_KEY'
})
]
})
【问题讨论】:
标签: angular typescript angular-material frontend recaptcha