首页 > 技术文章 > angular 实现总价满100折扣

java-php 2014-04-27 00:36 原文

<div ng-controller="CartController">
<div ng-repeat="item in items">
<span>{{item.title}}</span>
<input ng-model="item.quantity" />
<span>{{item.price|currency}}</span>
<span>{{item.price*item.quantity|currency}}</span>
</div>
<div>total:{{totalCart()|currency}}</div>
<div>Discount:{{bill.discount|currency}}</div>
<div>Subtotal:{{subtotal()|currency}}</div>
</div>

 

 

var app=angular.module("myApp",[]);
app.controller("CartController",function($scope){
$scope.bill={};
$scope.items=[{title:"paint pots",quantity:8,price:3.98},
{title:"polka dots",quantity:17,price:13.65},
{title:"pebbles",quantity:5,price:6.85},
{title:"ppts",quantity:18,price:33.09},
{title:"papses",quantity:98,price:45.68},
];
$scope.totalCart=function(){
var total=0;
for(var i=0,len=$scope.items.length;i<len;i++){
total=total+$scope.items[i].price*$scope.items[i].quantity;
}
return total;
}
$scope.subtotal=function(){
return $scope.totalCart()-$scope.bill.discount;

}
function calculateDiscount(newValue){
return $scope.bill.discount=newValue>1000?100:0;
}

$scope.$watch($scope.totalCart,calculateDiscount);
});

推荐阅读