Angular expression with several fields

advertisements

The title may not be right but here is what I want to happen.

I have this code here:

`{{cost.cost_a}}`

and

`{{cost.cost_b}}`

Both have values, I want it so that if only one has a value then it shows cost_b otherwise shows cost_a.

I was wondering if it would be an ng-if statement but I am unsure as to how this would work.

Sorry if i havent explained this correctly, If cost A has a value of 0 then show cost b otherwise show cost A


I would say the best way to do this is to have an expression that lives in your controller determining whether or not to show cost_a or cost_b.

// controller function
$scope.costDisplay = function costDisplayFn() {
  return cost.cost_b || cost.cost_a) ? cost.cost_b : cost.cost_a;
}

You can use this in your html to determine what displays.

<div>
 {{costDisplay()}}
</div>

This will keep the logic out of your DOM and in your controller where it belongs.