Angular - Check an option in the default selection dropdown

I am having trouble automatically setting the correct option as selected

. I suspect that the reason may be because of Angular not realizing that price.currency

and let currency of currencies

is the same object.

<select [(ngModel)]="price.currency" class="form-control">
    <option *ngFor="let currency of currencies" [ngValue]="currency">
        {{currency.iso_code}}
    </option>
</select>

      

I created model classes for price

and for currency

, as you can here:

export class ProductPrice {
    id: number;
    product: number;
    price: number;
    taxrule: Taxrule;
    currency: Currency;
}

      

When I select a list currencies

, I put it on a list Currency[]

.

Finally ... I also tried adding an argument [selected]="price.currency.id == currency.id"

to <option>

without any result. Probably because it ngValue

overrides it.

Ideas?

+3


source to share


1 answer


As you understood, price.currency

it cannot be recognized from an array currencies

. You can link these two values ​​by creating a link between the two. This can be done after you have received the currencies (s price

), for example, for example:

this.price.currency = this.currencies.find(x => x.id == this.price.currency.id)

      



Here Demo

+1


source







All Articles