[英]ERROR TypeError: Converting circular structure to JSON --> starting at object with constructor 'FormGroup'
I'm setting up Forms on Angular 7 and I got this error every time I enter the correct fields: name, email and phone. 我在Angular 7上设置表单,每次输入正确的字段(名称,电子邮件和电话)时都会收到此错误。 Then I click on the button
createEmployee
. 然后,我单击按钮
createEmployee
。 Normally it will pass to the next page, but this time it didn't and it show me this Error. 通常,它将传递到下一页,但是这次没有,并且向我显示此错误。
This is the html component: 这是html组件:
<div class="container custom-container">
<div class="col-md-12">
<h3 class="mb-3 text-center">Create Employee</h3>
<form [formGroup]="employeeDetails" (ngSubmit)="addEmployee()">
<div class="form-group">
<label>Name</label>
<input type="text" formControlName="name"
[(ngModel)]="employeeDetails.name" class = "form-control" [ngClass]="{
'is-invalid': submitted && f.name.errors }">
<div *ngIf="submitted && f.name.errors" class="invalid-feedback">
<div *ngIf="f.name.errors.required">Name is required
</div>
</div>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" formControlName="email"
[(ngModel)]="employeeDetails.email" class="form-control" [ngClass]="{
'is-invalid': submitted && f.email.errors }" />
<div *ngIf="submitted && f.email.errors" class="invalid-feedback">
<div *ngIf="f.email.errors.required">Email is required</div>
<div *ngIf="f.email.errors.email">Email must be a valid email
address</div>
</div>
</div>
<div class="form-group">
<label>Phone</label>
<input type="text" formControlName="phone"
[(ngModel)]="employeeDetails.phone" class="form-control" [ngClass]="{
'is-invalid': submitted && f.phone.errors }" />
<div *ngIf="submitted && f.phone.errors" class="invalid-feedback">
<div *ngIf="f.phone.errors.required">Phone is required</div>
</div>
</div>
<div class="form-group">
<button class="btn btn-success btn-lg btn-block">Create
Employee</button>
</div>
</form>
</div>
</div>
and this is the component.ts: 这是component.ts:
import { Component, OnInit , Input} from '@angular/core';
import { Router } from '@angular/router';
import { RestApiService } from "../shared/rest-api.service";
import { ReactiveFormsModule , FormsModule, FormControl} from
'@angular/forms'
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MustMatch } from '../helpers/must-match.validator';
@Component({
selector: 'app-employee-create',
templateUrl: './employee-create.component.html',
styleUrls: ['./employee-create.component.css']
})
export class EmployeeCreateComponent implements OnInit {
employeeDetails: FormGroup;
submitted = false;
constructor(
private formBuilder: FormBuilder,
public restApi: RestApiService,
public router: Router
) { }
ngOnInit() {
this.employeeDetails = this.formBuilder.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
phone: ['', Validators.required],
});
}
get f() { return this.employeeDetails.controls; }
addEmployee(dataEmployee) {
this.submitted=true;
if (this.employeeDetails.invalid) {
return;
}
this.restApi.createEmployee(this.employeeDetails).subscribe((data:
{}) => {
this.router.navigate(['/employees-list'])
})
alert('SUCCESS!! :-)\n\n' +
JSON.stringify(this.employeeDetails.value))
}
This is what i got when i click on createEmployee
: 这是我单击
createEmployee
时得到的:
First dont use [(ngModel)] and formControlName in the same time. 首先不要同时使用[[ngModel)]和formControlName。 It is obsolate as of Angular 6 I think.
我认为从Angular 6开始已经过时了。
You are trying to push a form in the request instead of an object this is why the problem occurs. 您试图在请求中而不是对象中推送表单,这就是为什么会出现此问题。 Try this:
尝试这个:
this.restApi.createEmployee(this.employeeDetails.getRawValue()).subscribe((data:
{}) => {
this.router.navigate(['/employees-list'])
})
alert('SUCCESS!! :-)\n\n' +
JSON.stringify(this.employeeDetails.getRawValue()))
Tell me how it went after that :) 告诉我那之后怎么样了:)
You can try this: 您可以尝试以下方法:
this.restApi.createEmployee(this.employeeDetails.value).subscribe((data:
{}) => {
this.router.navigate(['/employees-list'])
})
alert('SUCCESS!! :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.