Issue
I'm trying to select the first option after the user enters the email, but keeps not selected. Any solution?
HTML:
<label for="login"><b>Usuário:</b></label>
<input type="email" id="login" name="login" ngModel class="form-control" placeholder="E-mail"
required autofocus (focusout)="onSelect()">
<label><b>Empresa:</b></label>
<select ngModel name="IdEmpresa" class="form-control" id="IdEmpresa" disabled="{{ disabled }}">
<option *ngFor="let empresa of empresas; index as i" value="{{empresa.id}}"
>{{ empresa.nome }}</option>
</select>
TS:
onSelect() {
if ($("#login").val()== null || $("#login").val() =="") {
} else {
this.erro = false;
this.alertService.clear();
let email = (document.getElementById("login") as HTMLInputElement).value;
localStorage.setItem(constants.emailSafeguard, email);
this.service.getEmpresas(email).subscribe(lista => {
this.empresas = lista;
if(lista.length > 1) {
this.disabled = false;
} else {
this.disabled = true;
}
})
}
}
Solution
Your select has no value. Please change it like this:
<select [(ngModel)]="selectedEmpresaId" name="IdEmpresa" class="form-control" id="IdEmpresa" [disabled]="disabled">
<option *ngFor="let empresa of empresas; index as i" [value]="empresa.id"
>{{ empresa.nome }}</option>
</select>
And in ts (initial)
this.selectedEmpresaId = this.empresas[0].id;
Or with object instead of id
<select [(ngModel)]="selectedEmpresa" name="IdEmpresa" class="form-control" id="IdEmpresa" [disabled]="disabled">
<option *ngFor="let empresa of empresas; index as i" [value]="empresa"
>{{ empresa.nome }}</option>
</select>
And in ts (initial)
this.selectedEmpresa = this.empresas[0];
Answered By - MoxxiManagarm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.