Next, you add the helper functions, shown in Listing 28.22, to provide functionality for the AngularJS templates. The setContent()
function sets the $scope.content
value, effectively changing the view. The setProduct()
function is called when a user clicks a print image and sets the $scope.product
used in the product view. The cartTotal()
function iterates through the products in the user’s cart, updates $scope.shipping
, and returns a total that is then used in the cart and review views.
029 $scope.setContent = function(filename){
030 $scope.content = '/static/'+ filename;
031 };
032 $scope.setProduct = function(productId){
033 $scope.product = this.product;
034 $scope.content = '/static/product.html';
035 };
036 $scope.cartTotal = function(){
037 var total = 0;
038 for(var i=0; i<$scope.customer.cart.length; i++){
039 var item = $scope.customer.cart[i];
040 total += item.quantity * item.product[0].price;
041 }
042 $scope.shipping = total*.05;
043 return total+$scope.shipping;
044 };