Boolean in the Angular

Asked

Viewed 901 times

0

I have a class in Angular user call that gets 2 Boolean fields

export class UsuariosModel {
    UsuarioId: Number;
    Nome: string;
    Sobrenome: string;
    Email: string;
    Senha: string;
    Corretor: boolean;
    Contrato: boolean;   
}

On my page I have a form using formGroup and with all these fields, being the 'Broker' and the 'Contract' type:checkbox'. as follows...

<form [formGroup]="formUser" novalidate>
    <label>Nome</label>
    <input formControlName="nome" type="text" placeholder="Nome" />

    <label>Sobrenome</label>
    <input formControlName="sobrenome" type="text" placeholder="Sobrenome" />

    <label>Email</label>
    <input formControlName="email" type="text" placeholder="Email" />

    <label>Senha</label>
    <input formControlName="senha" type="text" placeholder="senha" />

    <label>corretor</label>
    <input formControlName="corretor" type="checkbox" />

    <label>Contrato</label>
    <input formControlName="contrato" type="checkbox" />

    <button (click)="inserindoUsuario(formUser.value)">Criar</button>
</form>

In my Typescript of this page, I have a method that creates an object of type 'User' and receives the parameters of formGroup. But if I do not check the checkbox it comes empty this way 'contract:""', and not as false this way 'contract:false'...

Here is my Formgroup and my method...

Formgroup

constructor(fb: FormBuilder, private api: ChamadaApi, http: Http){
    this.Http = http;

    this.formUser = fb.group({
        nome: new FormControl('', [Validators.required]),
        sobrenome: new FormControl('', [Validators.required]),
        email: new FormControl('', [Validators.required]),
        senha: new FormControl('', [Validators.required]),
        corretor: new FormControl(''),
        contrato: new FormControl('', [Validators.required]),
    });
}

Method

inserindoUsuario(usuarios: any){
    this.usuariosModel = {
         UsuarioId: null,
         Nome: usuarios.nome,
         Sobrenome: usuarios.sobrenome,
         Email: usuarios.email,
         Senha: usuarios.senha,
         Corretor: usuarios.corretor,
         Contrato: usuarios.contrato
     }

     this.api.CreateUser(this.usuariosModel);
}

1 answer

1


The quotes in Formcontrol are the initialization if you leave as quotes it will find that and string tries to set to false at the beginning

 corretor: new FormControl(false),
 contrato: new FormControl(false, [Validators.required]),
  • Booooa, perfect, worked perfectly...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.