Jose
Jose

Reputation: 1819

Select in Angular 4

I have in my view:

<select id="field_cliente" name="cliente"
        [(ngModel)]="oportunidad.cliente">
       <option [ngValue]="null"></option>
       <option [ngValue]="clienteOption" *ngFor="let clienteOption of clientes; trackBy: trackClienteById">{{clienteOption.razonSocial}}</option>
</select>

I have in my controller:

trackClienteById(index: number, item: Cliente) {
    return item;
}

The object is:

cliente{
       id:"",
       razonSocial:""
}

The value loads well when I select one, but when entering if it already has a previous value, it does not fit the value with the "select" and the "select" goes empty, even if the entity has value.

Upvotes: 0

Views: 177

Answers (1)

Jose
Jose

Reputation: 1819

<select  [compareWith]="equals" ...

in controller:

equals(o1: Cliente, o2: Cliente) { return o1.id === o2.id; }

Upvotes: 1

Related Questions