已解决我的表单控制器的值有问题。当我提交表单时,它们将保持为空字符串。我怀疑我可能在HTML方面犯了一个错误。
下面是register.component.ts
文件:
export class RegisterComponent implements OnInit {
constructor(
private userService: UserService,
private formBuilder: FormBuilder
) {}
registerFormGroup: FormGroup;
ngOnInit(): void {
this.registerFormGroup = this.formBuilder.group(
{
userName: ['', [Validators.required]],
},
);
}
submitted: boolean = false;
async onSubmit(user: User) {
this.submitted = true;
debugger;
if (this.registerFormGroup.invalid) return;
const result: Create_User = await this.userService.create(user);
if (result.succeeded) result.message, 'User registration successful.';
else result.message, 'User registration not successful.';
}
}
下面是register.component.html
文件:
<div class="register-form">
<div class="register-form-box">
<div class="register-form-header" class="form-group">
<h2>Sign Up</h2>
</div>
<form [formGroup]="registerFormGroup" (ngSubmit)="onSubmit(registerFormGroup.value)">
<div class="input-text">
<input type="text" formControlerName='userName' class="form-control" id="userName"
placeholder="Enter Username" [ngClass]="{'is-invalid': submitted}" />
</div>
<button type="submit">Submit</button>
</form>
</div>
</div>
谢谢你的帮助
解决我意识到,不知何故,我改变了html代码,这就是为什么它不工作
它应该像
formControlName='userName'
而不是
formControlerName='userName'
1条答案
按热度按时间3bygqnnd1#
进行了一些测试,无法复制错误,数据确实正确到达onSubmit函数。我怀疑该问题可能是由于表单值({userName:'asdasd'})和用户界面(大概是{name:string})。