从formBuilder中删除项目

时间:2018-03-20 17:48:56

标签: angular typescript firebase angular-forms

执行formBuild.group时,我创建了两个仅用于验证的值,这两个值不想保存在数据库中,我会在保存到数据库之前删除它们。

profile.component.ts:

profileForm: FormGroup;

constructor(){
    this.profileForm = this.createPerfilForm();
 }

createProfileForm() {
    return this.formBuilder.group({
        id: [this.perfil.id],
        name: [this.perfil.name, [Validators.required, Validators.minLength(5), Validators.maxLength(45)]],
        email: [this.perfil.email, [Validators.required, Validators.email]],
        password: [''],
        passwordConfirm: ['', [confirmPassword]],
    });
}

saveProfile(){
     // I need to remove here password and passwordConfirm 
     //before saving to the database
     this.authService.updateProfile(this.profileForm.value);
}

我需要从this.profileForm.valuepassword值中删除passwordConfirm,因为我不想将这些值保存在数据库中。

2 个答案:

答案 0 :(得分:2)

saveProfile(){
     // I need to remove here password and passwordConfirm 
     //before saving to the database
     let copy = { ... this.profileForm.value };
     delete copy.password;
     delete copy.confirmPassword;
     this.authService.updateProfile(copy);
}

试试这个吗?

答案 1 :(得分:0)

只使用您需要的东西创建一个新对象:

 this.authService.updateProfile({id: this.profileForm.value.id, 
           name: this.profileForm.value.name, email:this.profileForm.value.email })