从 formBuilder 中删除项目

remove item from formBuilder

执行时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);
}

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

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);
}

试试这个?

只用你需要的东西制作一个新对象:

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