如何解决Angular Reactive Forms:如何使表单值不进入后端
我正在实施一个注册表单(反应式)。其中我有密码和确认密码。我正在验证这两个字段,但在提交时我只想将密码字段转到 spring boot。有人知道怎么做吗?
//registration.ts
registrationForm: FormGroup;
constructor(private fb: FormBuilder,private authService: AuthService,private router: Router) {}
ngOnInit() {
this.registrationForm = this.fb.group({
fullname: ["",[Validators.required,Validators.minLength(5)]],emailId: ["",password: [null,Validators.required],confirmPassword: [null,phoneNumber: [null,enabled: [true],roles:this.fb.array([{'role':'ADMIN'}]),address: this.fb.group({
city: [""],state: [""],country: [""],postalCode: [""],}),},//{ validator: this.ConfirmPasswordValidator("password","confirmPassword")}
{ validator: this.checkPasswords}
);
}
checkPasswords (group : FormGroup) {
let pass = group.get('password').value;
let confirmPass = group.get('confirmPassword').value
return pass === confirmPass ? null : { passwordNotSame: true }
}
RegisterUser(form: NgForm) {
this.authService.register(form)
.subscribe(
result => {
if (result.token) {
localStorage.setItem('token',result.token)
this.router.navigate(['dashboard'])
}
}
)
}
请大家给点意见。我是角度的新手
解决方法
这不是关于 Angular,而是关于 Javascript
添加
RegisterUser(form: NgForm) {
const {confirmPassword,...rest } = form;
this.authService.register(rest)
.subscribe(
,
然后从 FormGroup 中删除特定值,如下所示。希望它会起作用
RegisterUser(form: NgForm) {
delete this.registerForm.value.confirmPassword;
this.authService.register(form)
.subscribe(
result => {
if (result.token) {
localStorage.setItem('token',result.token)
this.router.navigate(['dashboard'])
}
}
)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。