实现购物车功能

这个示例展示了如何利用AngularJS创建一个简单的购物车功能。包括商品的增删、数量调整、总价计算等操作。用户可以增加或减少商品数量,点击删除按钮移除商品,或者批量删除所有商品。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html ng-app="App">
<head>
<meta charset="utf-8" />
<title></title>
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script type="text/javascript">
var App=angular.module("App",[]);
App.controller("Democtrl",function($scope){

$scope.datas=[
{
id:2001,
shopname:"iPhoneX",
count:3,
price:8699,
       }, 
 {
id:2002,
shopname:"iPhone8",
count:4,
price:6699,
       }
 , 
 {
id:2003,
shopname:"iPhone7p",
count:2,
price:5699,
       }
 , 
 {
id:2004,
shopname:"iPhone7",
count:1,
price:5099,
       }
];
$scope.del=function($index){
 $scope.datas.splice($index,1);
}
$scope.od=function(){
for(var i=$scope.datas.length;i>=0;i--){
$scope.datas.splice(i,1);
}
}
$scope.counts=function(){
var a=0;
for(var i=0;i<$scope.datas.length;i++){
a+=$scope.datas[i].count;
}
return a;
}

$scope.allSum=function(){
var allprice=0;
for(var i=0;i<$scope.datas.length;i++){
allprice+=$scope.datas[i].count*$scope.datas[i].price;

}
return allprice;
}
})
</script>
</head>
<body ng-controller="Democtrl">
<table border="1" cellspacing="0" cellpadding="1">
<tr>
<td>产品编号</td>
<td>产品名称</td>
<td>购买数量</td>
<td>产品单价</td>
<td>产品总价</td>
<td>操作</td>

</tr>
<tr ng-repeat="x in datas">
<td>{{x.id}}</td>
<td>{{x.shopname}}</td>
<td><input type="button" value="-" ng-click="x.count=x.count-1"/><input type="text" id="" value="{{x.count}}" /><input type="button" value="+" ng-click="x.count=x.count+1"/></td>
<td>{{x.price}}</td>
<td>{{x.price*x.count}}</td>
<td><input type="button" ng-click="del($index);" value="删除" /></td>
</tr>
<tr >
<td></td>
<td>购买总数</td>
<td>{{counts()}}</td>

<td>购买总价</td>
<td>{{allSum()}}</td>
<td><input type="button" ng-click="od();" value="批量删除" /></td>
</tr>
</table>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值