I'm updating my form
way to do it in Angular 7 I've been reading that I need to import app.module.ts
this : import { ReactiveFormsModule } from '@angular/forms';
and put it in my imports
:
imports: [
ReactiveFormsModule
],
Then in my view make use of the new form attribute formControlName
, an example would look something like this:
<input type="email" formControlName="email" class="form-control" name="email">
Of course, for the view attribute to work, I go to my file.component.ts and import :
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
I then inject it via the constructor:
constructor(private ps:PaisesService,private formBuilder: FormBuilder) {
this.usuario = new Usuario('','','','','','','','default_image.jpg');
}
My next step is to create, let's say, the configuration of my form
new Angular 7form
gives the possibility to decouple the view and do practically everything from it thanks to the following that I show:fichero.component.ts
//nuevo form (funcion)
private buildForm(){
this.formGroup = this.formBuilder.group({
nombre_real:'',
apellidos : '',
email :'',
nombre_usuario : '',
password : '',
repetir_password:'',
img_perfil : 'img_perfil',
pais : '',
});
With this, I allow my controls ( ) to be bound to the form of my view , replacing it, thus giving it greater independence.inputs
[(ngModel)]
And now we just have to put that works inside the ngOnInit()
such that like this:
ngOnInit() {
this.cargarPaises();
//nuevo form
this.buildForm();
}
Put in situation I go to my problem :
The bindings of this new form ( this.formBuilder.group({})
) work great BUT when I try to bind my input
type File
it does not let me set a String
, this is what I mean:
private buildForm(){
this.formGroup = this.formBuilder.group({
nombre_real:'',
apellidos : '',
email :'',
nombre_usuario : '',
password : '',
repetir_password:'',
//Este 'img_perfil' es un input de type File, pero no me acepta Objetos File ni String
img_perfil : 'img_perfil',
pais : '',
});
Finally I ask:
How do I put something by default that admits it to me and makes a minimum of sense by saying that when it is verified I am only interested in knowing if there is or there is not a file uploaded?
I think you should initialize it to
null
and then check in the check if it isnull
or not.Cheers