angular实现一个购物车

html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../vendor/bootstrap-3.4.1-dist/css/bootstrap.min.css">
</head>

<script src="../vendor/angular/angular.js"></script>
<script src="app/index.js"></script>

<body ng-app="myapp">
    <div id="container" ng-controller="cartController">
        <table class="table  table-hover" ng-show="cart.length">
            <thead>
                <th>产品编号</th>
                <th>产品名字</th>
                <th>购买数量</th>
                <th>产品单价</th>
                <th>产品总价</th>
                <th>操作</th>
            </thead>
            <tbody>
                <tr ng-repeat="item in  cart">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>
                        <button type="button" ng-click="redue(item.id)" class="btn btn-info">-</button>
                        <input type="number" value="{{item.quantity}}" ng-model="item.quantity">
                        <button type="button" ng-click="add(item.id)" class="btn btn-info">+</button>
                    </td>

                    <td>{{item.price}}</td>
                    <td>{{item.quantity*item.price}}</td>
                    <td>
                        <button type="button" ng-click="remove(item.id)" class="btn btn-danger">移除</button>
                    </td>
                </tr>
                <tr>
                    <td>
                        总数:
                    </td>
                    <td>{{totalPrice()}}</td>
                    <td>总购买数:</td>
                    <td>{{totalQuantity()}}</td>
                    <td colspan="2" style="text-align: left">
                        <button type="button" ng-click="cart={}" class="btn btn-danger">清空购物车</button></td>

                </tr>
            </tbody>
        </table>
        <p ng-show="!cart.length">你的购物车为空!</p>
    </div>

</body>

</html>

js:

var app = angular.module("myapp", []);
app.controller("cartController", function($scope) {
    $scope.cart = [{
                id: 1000,
                name: "iphone5s",
                quantity: 3,
                price: 4300
            },
            {
                id: 3300,
                name: "iphone5",
                quantity: 43,
                price: 3300
            },
            {
                id: 232,
                name: "imac",
                quantity: 56,
                price: 23000
            },
            {
                id: 1002,
                name: "ipad",
                quantity: 65,
                price: 6900
            }
        ]
        // 计算购物车总价
    $scope.totalPrice = function() {
        var total = 0;
        // $scope.cart.forEach(item => {
        //     total += item.price * item.quantity;
        // });
        angular.forEach($scope.cart, item => {
            total += item.price * item.quantity;
        });

        return total;
    }

    // 计算总购买数
    $scope.totalQuantity = function() {
        var total = 0;
        angular.forEach($scope.cart, item => {
            total += item.quantity;
        })

        return total;
    }

    //添加一个数量
    $scope.add = function(id) {
            var index = findIndex(id);
            if (index != -1) {
                ++$scope.cart[index].quantity;
            }

        }
        //减去一个数量
    $scope.redue = function(id) {
        var index = findIndex(id);
        if (index != -1) {
            var item = $scope.cart[index];
            if (item.quantity > 1) {
                --item.quantity;
            } else {
                var returnkey = confirm("从购物车内删除该产品?");
                if (returnkey) {
                    $scope.remove(id);
                }
            }

        }
    }


    //找到一个索引
    var findIndex = function(id) {
        var index = -1;
        angular.forEach($scope.cart, function(item, key) {
            if (item.id === id) {
                index = key
            }

        })

        return index;
    }


    //删除事件
    $scope.remove = function(id) {
        var index = findIndex(id);
        if (index !== -1) {
            $scope.cart.splice(index, 1);
        }
    }


    ///监听数据

    $scope.$watch('cart', function(newValues, oldValues) {
        console.log(newValues);
        angular.forEach($scope.cart, (item, key) => {
            if (item.quantity < 1) {
                var returnkey = confirm("从购物车内删除该产品?");
                if (returnkey) {
                    $scope.remove(item.id);
                } else {
                    item.quantity = oldValues[key].quantity;
                }
            }
        })
    }, true)

})

效果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值