angular购物车

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title></title>

<script type="text/javascript" src="../js/angular.min.js" ></script>
<script>

var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.shoplist = [
{id:'1001',name:'iPhone4s',price:'2000',num:1},
{id:'2001',name:'iPhone5s',price:'3650',num:3},
{id:'3001',name:'iPhone6',price:'8000',num:6},
{id:'4001',name:'iPhone8',price:'9645',num:9},
]

//减少数量函数
$scope.jian = function(index){
var num = $scope.shoplist[index].num;
if(num >1){
$scope.shoplist[index].num--;
}else{
if(confirm("再减就需要移除购物车")){
$scope.shoplist.splice(index,1);
}
}
}

//添加数量函数
$scope.add = function(index){
$scope.shoplist[index].num++;
}

//计算购买数量
$scope.nums = function(){
var num = 0;
for(var i = 0;i<$scope.shoplist.length;i++){
num+=$scope.shoplist[i].num;
}
return num;
}

//计算购买总价
$scope.prices = function(){
var price = 0;
for(var i = 0;i<$scope.shoplist.length;i++){
price+=$scope.shoplist[i].price*$scope.shoplist[i].num;
}
return price;
}

//实现移除功能
$scope.remove = function(index){
if(confirm("确定要移除此项?")){
$scope.shoplist.splice(index,1);
}
}

//实现全部移除
$scope.removes = function(){
if(confirm("确定要移除购物车?")){
$scope.shoplist = [];
}
}

})



</script>
</head>
<body ng-app="myApp">
<center>
<table ng-controller="myCtrl" border="1";>

<tr align="center">
<td>产品编号</td>
<td>产品名称</td>
<td>购买数量</td>
<td>产品单价</td>
<td>产品总价</td>
<td>操作</td>
</tr>
<tr  align="center" ng-repeat="shop in shoplist">
<td>{{shop.id}}</td>
<td>{{shop.name}}</td>
<td>
<button ng-click="jian($index)">-</button>
<input type="text" placeholder="请输入大于0的数字" ng-model="shop.num" ng-change="change($index)"/>
<button ng-click="add($index)">+</button>
</td>
<td>{{shop.price}}</td>
<td>{{shop.price*shop.num}}</td>
<td><button ng-click="remove($index)">移除</button></td>
</tr>
<tr>
<td ></td>
<td>购买数量</td>
<td ng-bind="nums()"></td>
<td>购买总价</td>
<td ng-bind="prices()"></td>
<td><button ng-click="removes()">移除全部</button></td>
</tr>
</table>
</center>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值