Reputation: 13915
Currently in the view the totalCost
is passed as a function to round
, but it has to be integer value.
View
{{round(shoppingCart.totalCost)}}
Factory
myApp.factory( ....
var calculateTotalCost = function(){
return this.items.map(function(item){
return HelperFunctions.getPrice(item) * item.quantity;
}).reduce(function(p, c){
return p + c;
});
};
var factory = {
shoppingCart: {order: currentOrder, totalCost: calculateTotalCost}
}
return factory;
}
How to solve it?
round : function (value) {
return typeof value !== 'undefined'? value.toFixed(2): 0;
}
Upvotes: 0
Views: 37
Reputation: 136144
Currently you had totalCost
variable which hold reference of calculateTotalCost
function there in shoppingCart
object. So while showing the same variable on view, you need to have service reference make available over the view by doing $scope.shoppingCart = shoppingCart
You could use number
filter itself with precision 2
in your case, as you wanted to make the value fixed to precision 2
. That will also help you to show the number in currency format.
{{shoppingCart.totalCost() | number:2 }}
Upvotes: 1
Reputation: 21844
You should avoid to call a function from a factory directly in a view expression.
In your directive's controller:
getTotalCost
(which hides calculateTotalCost
)Upvotes: 0