Angular 我的表单在提交列表中返回空值
我正在和Angular一起做一个个人项目。我占用了表单,但是当我使用提交按钮提交表单时,它的值为空。 这是HTML代码 在TS代码中,实现一些console.log以查看每个变量的值,但它们都向我返回nullAngular 我的表单在提交列表中返回空值,angular,typescript,forms,Angular,Typescript,Forms,我正在和Angular一起做一个个人项目。我占用了表单,但是当我使用提交按钮提交表单时,它的值为空。 这是HTML代码 在TS代码中,实现一些console.log以查看每个变量的值,但它们都向我返回null 代码的问题在于重置表单调用。它将表单控件pokeid设置为null 您应该按以下方式更改代码 onSubmit(event: Event) { event.preventDefault(); if (this.checkoutform.valid) { var
代码的问题在于重置表单调用。它将表单控件pokeid设置为null 您应该按以下方式更改代码
onSubmit(event: Event) {
event.preventDefault();
if (this.checkoutform.valid) {
var poked = this.checkoutform.value;
this.pokeservice.getPokebyID(poked).subscribe(newPoke => {
console.log(newPoke);
this.checkoutform.reset();
});
} else {
console.log(this.checkoutform.value);
console.log(poked.pokeid);
console.log("error");
}
}
}
您可以在api调用订阅块中重置表单。谢谢兄弟!你是最棒的;D智利的问候
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { PokeServiceService} from './../../../core/services/poke-service.service';
@Component({
selector: 'app-bar-search',
templateUrl: './bar-search.component.html',
styleUrls: ['./bar-search.component.scss']
})
export class BarSearchComponent implements OnInit {
checkoutform;
constructor(
private formbuilder: FormBuilder,
private pokeservice: PokeServiceService
) {
this.buildForm();
}
ngOnInit(): void {
}
private buildForm() {
this.checkoutform = this.formbuilder.group({
pokeid: ['', [Validators.required, Validators.pattern('^[0-9]*$')]],
});
}
get pokeidField() {
return this.checkoutform.get('pokeid');
}
onSubmit(event: Event) {
event.preventDefault();
this.checkoutform.reset();
if (this.checkoutform.valid) {
var poked = this.checkoutform.value;
this.pokeservice.getPokebyID(poked)
.subscribe((newPoke) => {
console.log(newPoke);
});
} else {
console.log(this.checkoutform.value);
console.log(poked.pokeid);
console.log('error');
}
}
}
onSubmit(event: Event) {
event.preventDefault();
if (this.checkoutform.valid) {
var poked = this.checkoutform.value;
this.pokeservice.getPokebyID(poked).subscribe(newPoke => {
console.log(newPoke);
this.checkoutform.reset();
});
} else {
console.log(this.checkoutform.value);
console.log(poked.pokeid);
console.log("error");
}
}
}