0

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.

Jose
  • 1,779
  • 4
  • 26
  • 50
  • How to you declare Cliente class ? I set up a demo, you can correct to your example: https://stackblitz.com/edit/angular-kr2ten?file=app/app.component.html – Vega Oct 19 '17 at 07:43
  • 1
    Thanks for your help, in the end I was given an example by a friend. It's a great help for the link you sent me to try again. – Jose Oct 19 '17 at 12:38
  • Possible duplicate of [Angular 2, set default value to select option](https://stackoverflow.com/questions/39724161/angular-2-set-default-value-to-select-option) – AT82 Oct 20 '17 at 11:18

1 Answers1

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

in controller:

equals(o1: Cliente, o2: Cliente) { return o1.id === o2.id; }
Jose
  • 1,779
  • 4
  • 26
  • 50