Jonathan Solorzano
Jonathan Solorzano

Reputation: 7022

interchangeable select as in ng-options

I want to know how to make the select as of the ng-options attribute interchangeable, I mean, I want it to evaluate if display one or another value; there's an array of jsons that has the following keys:

[
    {id: 1, name: 'christmas', alias: 'xmas'},
    {id: 2, name: 'new year', alias: null}
]

So, what I want is to display name if alias is null, but if alias is not null, then display alias, what do I need to add to ng-options:

ng-options="f as f.name for f in vm.festivities"

Upvotes: 0

Views: 456

Answers (2)

Riste Spaseski
Riste Spaseski

Reputation: 1

Try this:

ng-options="f as f.alias && (f.name === null || f.name) for f in vm.festivities"

because you want f.alias to be not null and other expression can be null

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

Use

ng-options="f as (f.alias || f.name) for f in vm.festivities"

In JavaScript, a || b evaluates to the first truthy operand (or the last one if none of them is truthy)

Upvotes: 3

Related Questions