I have a class in the Angular call user that receives 2 boolean fields
export class UsuariosModel {
UsuarioId: Number;
Nome: string;
Sobrenome: string;
Email: string;
Senha: string;
Corretor: boolean;
Contrato: boolean;
}
In my page I have a form using formGroup and all of these fields, being the 'Broker' and 'Contract' of '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 thus' 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);
}