[英]Angular (ngSubmit) form does not work, How can I do it?
I tried very hard but it didn't work.我非常努力地尝试但没有奏效。 I am not getting any error whatsoever.
我没有收到任何错误。 My data is not being saved.
我的数据没有被保存。 But I don't get any errors either.
但我也没有收到任何错误。 I do not take any action when I press the button.
当我按下按钮时,我没有采取任何行动。 I control everything.
我控制一切。 I wonder if there is an error in using the ng-model.
我想知道使用ng-model是否有错误。 Backend is also OK.
后端也可以。 Registration on post
邮寄登记
HTML HTML
<form #myForm="ngForm" class="form-container" (ngSubmit)="onSubmit()"
[formGroup]="bookForm">
<input placeholder="Kategori seç"
ng-model="categoryBy" type="text" class="form-control form-control-lg">
<input ng-model="name" type="text" class="form-control form-control-lg">
<button type="submit">save</button>
</form>
TS TS
onSubmit() {
if (this.bookForm.valid) {
if (this.type == "add") {
this.bookService
.saveBookImage(this.formData)
.pipe(
map(result => {
this.bookForm.controls.picture.setValue(result.url);
}),
mergeMap(() => this.bookService.addBook(this.bookForm.value))
)
.subscribe(result => {
this.router.navigateByUrl("/general/blank-page");
});
} else {
if (this.formData == null) {
this.bookService
.updateBook(this.book._id, this.bookForm.value)
.subscribe(result => {
this.router.navigateByUrl("/general/blank-page");
});
} else {
this.bookService
.saveBookImage(this.formData)
.pipe(
map(result => {
this.bookForm.controls.picture.setValue(result.url);
}),
mergeMap(() =>
this.bookService.updateBook(this.book._id, this.bookForm.value)
)
)
.subscribe(result => {
this.router.navigateByUrl("/general/blank-page");
});
}
}
}
}
ngOnInit() {
this.categoryService.getCategories().subscribe(result => {
this.categories = result;
});
this.bookId = this.route.snapshot.paramMap.get("id");
if (this.bookId == null) {
this.title = "Kitap Ekleme";
this.btnText = "Ekle";
this.type = "add";
} else {
this.title = "Kitap Güncelleme";
this.btnText = "Güncelle";
this.type = "update";
this.bookService.getBookById(this.bookId).subscribe(result => {
this.book = result;
this.bookForm.controls.name.setValue(this.book.name);
this.bookForm.controls.author.setValue(this.book.author);
this.bookForm.controls.price.setValue(this.book.price);
this.bookForm.controls.stock.setValue(this.book.stock);
this.bookForm.controls.picture.setValue(this.book.picture);
this.bookForm.controls.categoryBy.setValue(this.book.categoryBy);
});
}
this.bookForm = new FormGroup({
name: new FormControl("name", Validators.required),
categoryBy: new FormControl("", Validators.required)
});
} }
You seem to have mixed up AngularJS syntax with Angular 2+ syntax.您似乎混淆了 AngularJS 语法和 Angular 2+ 语法。
ng-model
was used in AngularJS. You also seem to be using reactive forms, so you should be using formControlName
in the first place. ng-model
在 AngularJS 中使用。你似乎也在使用反应式 forms,所以你应该首先使用formControlName
。
Change your ng-model
to formControlName
and you should be able to trigger onSubmit
.将您的
ng-model
更改为formControlName
并且您应该能够触发onSubmit
。
<form #myForm="ngForm" class="form-container" (ngSubmit)="onSubmit()" [formGroup]="bookForm">
<input placeholder="Kategori seç" formControlName="categoryBy" type="text" class="form-control form-control-lg">
<input formControlName="name" type="text" class="form-control form-control-lg">
<button type="submit">save</button>
</form>
I would also highly recommend going through Reactive Forms and NgForm .我还强烈建议您浏览Reactive Forms和NgForm 。
Based on your code, you are using a Template-Driven Form
which uses ngModel
根据您的代码,您正在使用使用
ngModel
的Template-Driven Form
Have created a Stackblitz Demo for your reference.已创建Stackblitz Demo供您参考。
<form #myForm="ngForm" // If you are using a template form, no need to implement the reactive form which is [form] or [formGroup]
class="form-container"
(ngSubmit)="onSubmit(myForm.value)"> // Pass your form value, since you've specified #myForm="form"
// assigning your form ID with "myForm", use that id as a reference
// for your form to fetch it's overall form value
<input placeholder="Kategori seç"
name="categoryBy" // when using ngModel be sure to add the 'name' property
[(ngModel)]="categoryBy" // instead of ng-model, use the two-way data binding [(ngModel)]
...>
...
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.