Reputation: 1024
I have a variable budget
. I have a switch that displays budget
in gallons or barrels. It is in gallons by default. There are 42 gallons in a barrel. So when I switch to barrels I display the value divided by 42. Here's what it looks like in angular:
<td class="text-right">{{barrels ? (fuelBudgets[0].budget/42 | number:1) : (fuelBudgets[0].budget | number:0)}}</td>
But if budget
is null
and it is divided by 42 I get 0 as a result but I want the result to stay as null. How can I do this so the code will handle the numbers like I want but if the value is null it will return null?
thanks
Upvotes: 1
Views: 1666
Reputation: 1606
I would add a helper method that checks if there is a null input:
var helper = function(x, y) {
if(null != x) {
return x / y
}
return null
}
Basic idea: check if numerator or denominator are null. If they are null return null, otherwise return numerator / denominator
Upvotes: 1
Reputation: 931
You just need to "double check" before output, try this:
{{barrels ? (fuelBudgets[0].budget/42 == 0 ? NULL : fuelBudgets[0].budget/42) : (fuelBudgets[0].budget)}}
Or this:
budget = fuelBudgets[0].budget/42;
{{barrels ? (budget == 0 ? NULL : budget) : fuelBudgets[0].budget}}
Upvotes: 1