angular JS购物车删除增加

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车</title>


    <script src="angular.min.js"></script>
    <script src="jquery-3.2.1.min.js"></script>
    <style>
        .shanchu{
            background-color: aqua;
        }
        .jiahao{
            background-color: aqua;
        }
        .jianhao{
        background-color: aqua;
        }
        .qingkong{
            background-color: red;
        }
    </style>


    <script>
        var myapp=angular.module("myapp",[]);
        myapp.controller("myCtrl",function ($scope) {
            $scope.arr=[
                {name:"苹果4", price:12.90,quantity:2,totalprice:25.80},
                {name:"苹果5", price:15.00,quantity:1,totalprice:15.00},
                {name:"苹果6", price:20.00,quantity:2,totalprice:20.00},
                {name:"苹果7", price:25.0,quantity:2,totalprice:25.00}
            ]
        //减少数量
            $scope.reduce=function (index) {
                if($scope.arr[index].quantity>1){
                    $scope.arr[index].quantity--;


                }else{
                    $scope.remove(index);
                }
            }
            $scope.add = function(index){
                $scope.arr[index].quantity++;
            }
            //删除
            $scope.remove = function(index){
                if(confirm("确定要删除当前商品么?")){
                    $scope.arr.splice(index,1)
                }
            }
            //清空购物车
            $scope.removeAll = function(){
                if(confirm("你确定清空购物车所有商品吗?")){
                    $scope.arr  = [];
                }
            }
            //点击到1时删除
            $scope.change = function(index){
                if ( $scope.Product[index].quantity >= 1) {


                }else{
                    $scope.Product[index].quantity = 1;
                }


            }
            //购买的总数
            $scope.numAll = function(){
                var numAlls = 0
                for(var i = 0 ; i <$scope.arr.length;i++ ){
                    numAlls += $scope.arr[i].quantity;
                }
                return numAlls;
            }
            //购买的总价
            $scope.totalQuantity = function(){
                var allprice = 0
                for(var i = 0 ; i <$scope.arr.length;i++ ){
                    allprice += $scope.arr[i].quantity *  $scope.arr[i].price;
                }
                return allprice;
            }


        })
    </script>




</head>
<body ng-app="myapp">
<h2>我的购物车</h2>
<div ng-controller="myCtrl">
    <table border="1px">
        <tr>
            <th>商品名称</th>
            <th>商品价格</th>
            <th>商品数量</th>
            <th>商品总价</th>
            <th>操作</th>
        </tr>
        <tr  ng-repeat="item in arr" >
            <td>{{item.name}}</td>
            <!--价格前面加符号-->
            <td>{{item.price|currency:"¥"}}</td>
            <td>
                <button ng-click="reduce($index)" class="jianhao">-</button>
                <input  type="text" class="num" ng-model="item.quantity" ng-change="change($index)" />
                <button ng-click="add($index)" class="jiahao">+</button>
            </td>
            <td>{{item.price*item.quantity|currency:"¥"}}</td>
            <td><button ng-click="remove($index)" class="shanchu">删除</button></td>
        </tr>


    </table>
    <div id="foot"><span>总价为:</span><span ng-bind="totalQuantity()"></span><span>购买数量</span>
        <span >{{numAll()}}</span>
        <button ng-click="removeAll()" class="qingkong">清空购物车</button>
    </div>


</div>






</body>
</html>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值