Jezcentral
Jezcentral

Reputation: 412

How do I bind one of two different values in Angular?

I need the data to be one of two possible values, depending on which actually exists. Let's say I have a list of TVs and I want to store that value of the "name" of myTVs[0] and myTVs[1].

E.g. var myTVs = [ { "JapaneseTV": { "name":"Sony" }, "TaiwaneseTV":{} }, { "JapaneseTV":{}, "TaiwaneseTV": { "name":"Samsung" } } ];

Is there a way to bind the data in Angular using the equivalent of:

<p ng-repeat="tv in myTVs">{{tv.name}}</p>

where tv.name = JapaneseTV.name || TaiwaneseTV.name; // I.e. I don't have to check which one to use, it just picks the one that returns a value.

Upvotes: 0

Views: 39

Answers (2)

Banana-In-Black
Banana-In-Black

Reputation: 2557

It might looks simpler like

<p ng-repeat="tv in myTVs">{{tv.JapaneseTV.name || tv.TaiwaneseTV.name}}</p>

since you got tv in myTVs

Upvotes: 1

Jezcentral
Jezcentral

Reputation: 412

<p ng-repeat="tv in myTVs">{{myTVs[$index].JapaneseTV.name || myTVs[$index].TaiwaneseTV.name}}</p>

Upvotes: 0

Related Questions