pra*_*wal 3 angular angular-reactive-forms
我在 Angular 8 中使用反应式表单将数据插入数据库,但收到此错误TypeError: Converting circular structure to JSON。据说name财产是一个循环结构,我无法找出原因。谁能帮我吗?提前致谢。
我的组件.ts
export class AddMainCategoryComponent implements OnInit {
nm;
desc;
date;
get name() {
return this.addMCform.get('name')
}
get description() {
return this.addMCform.get('description')
}
constructor(public mainCategoryService: MainCategoryService, private fb: FormBuilder) { }
addMCform = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]],
description: ['']
})
ngOnInit() {
}
addMain_categories() {
let main_category = {
nm: this.name,
desc: this.description,
date: new Date(),
}
this.mainCategoryService.addMain_categories(main_category).then(data => {
console.log(data);
})
}
}
Run Code Online (Sandbox Code Playgroud)
html
<div class="container-fluid">
<h2>Add main category</h2>
<form [formGroup]="addMCform">
<div class="form-group">
<label>Name of main category</label>
<input [class.is-invalid]="name.invalid && name.touched" formControlName="name" type="text"
class="form-control" placeholder="Enter name of category">
<div *ngIf="name.invalid && name.touched">
<small *ngIf="name.errors?.required" class="text-danger"> name is required</small>
<small *ngIf="name.errors?.minlength" class="text-danger"> name must be at least 3 characters </small>
</div>
</div>
<div class="form-group">
<label>Description:</label>
<textarea class="form-control" rows="5" input type="textarea" formControlName="description"
placeholder="Enter description"></textarea>
</div>
<button type="button" class="btn btn-primary" (click)="addMain_categories()">Add main category</button>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
this.name返回的this.addMCform.get('name')是一个表单控件。同样适用于this.description. 在 中main_category,您需要传递value而不是整个表单控件。
let main_category = {
nm: this.name.value,
desc: this.description.value,
date: new Date()
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5453 次 |
| 最近记录: |