anglular-动态购物车-watch-index

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title></title>
<script src="../4月24日/js/angular.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrop/bootstrap.min.css"/>
<script src="js/bootstrop/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body ng-controller="myci">
<div class="container-fluid">
<div class="row col-md-6 col-md-offset-3" >
<table class="table table-bordered">
<tr>
               <th>#</th>
               <th>商品名称</th>
               <th>商品单价</th>
               <th>购买数量</th>
               <th>小计金额</th>
               <th ng-click="dle()">删除</th>                
</tr>
<tr ng-repeat="good in goodses">
<td>{{$index+1}}</td>
<td>{{good.goodsName}}</td>
<td>{{good.goodsPrice}}</td>
<td><input type="text" ng-model="good.count"/></td>
<td>{{good.goodsPrice*good.count}}</td>
               <td ng-click="dle($index)">删除</td>
</tr>
</table>
<div>
<p>购买金额小于100元运费为10元<br/>购买金额小于300元运费为20元<br/>购买金额大于300元运费为0元</p>
<p>合计金额为:<span>{{a1}}</span></p>
<p>运费:<span>{{a2}}</span></p>
<p>应付款:{{a3}}</p>
</div>
</div>
</div>
</body>
<script type="text/javascript">
var app=angular.module("myApp",[]);
app.controller("myci",["$scope",function($scope){
$scope.goodses=[
                  {goodsID:1,goodsName:"寒冰",goodsPrice:14,count:1,subtotal:14},
                  {goodsID:2,goodsName:"龙龟",goodsPrice:12,count:1,subtotal:12},
                  {goodsID:3,goodsName:"德玛",goodsPrice:9,count:1,subtotal:9},
                  {goodsID:4,goodsName:"剑圣",goodsPrice:40,count:1,subtotal:40},
                  {goodsID:5,goodsName:"螳螂",goodsPrice:19,count:1,subtotal:19}
              ]
$scope.$watch("goodses",function(){
$scope.a1=0;
$scope.a2=0;
$scope.a3=0;
for (var i=0;i<$scope.goodses.length;i++) {
$scope.goodses[i].subtotal=$scope.goodses[i].goodsPrice*$scope.goodses[i].count;
$scope.a1+=$scope.goodses[i].subtotal;
if ($scope.a1<100) {
$scope.a2=10;
} else if($scope.a1<300){
$scope.a2=20;
}else{
$scope.a2=0;
}
}
$scope.a3=$scope.a1+$scope.a2;


},true)
$scope.dle=function(d){
$scope.goodses.splice(d,1)
}

}]);
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值