今天给大家分享一个购物车的例子,希望对大家有所帮助

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../angular-1.5.5/angular.js"></script>
    <style>
        button{
            background: red;
        }
    </style>
    <script>
        var my=angular.module("my",[]);
        my.controller("mys",function ($scope) {
            /*随意添加数据*/
            $scope.data=[
                {name:"iPhone8",count:1,price:"8888"},
                {name:"iPhone9",count:1,price:"9888"},
                {name:"iPhone2s",count:1,price:"3888"},
                {name:"iPhone7p+",count:1,price:"10088"}
                ]
            /*删除一个*/
            $scope.del=function (index) {
                if(confirm('确定移除此项嘛?')){
                    $scope.data.splice(index,1);
                }
            };

            //增加
            $scope.incr = function(index){
                $scope.data[index].count++;
            }
            //减少
            $scope.decr = function(index){
                if($scope.data[index].count > 1){
                    $scope.data[index].count--;
                }
            }
            /*总价格*/
            $scope.allSum=function () {
                var allPrice=0;
                for(var i=0;i<$scope.data.length;i++){
                    allPrice+=$scope.data[i].price*$scope.data[i].count;
                }
                return allPrice;
            };
            /*总个数*/
            $scope.allNum=function () {
                var allnumber=0;
                for(var i=0;i<$scope.data.length;i++){
                    allnumber+=$scope.data[i].count;
                }
                return allnumber;
             };
            /*清空购物车*/
            $scope.qingkong=function () {
                if($scope.data.length==0){
                    alert('你的购物车为空');
                }else{
                    $scope.data=[];
                }
            }
        })
    </script>
</head>
<body ng-app="my" ng-controller="mys">
    <table border="1" width="600">
        <tr>
            <th>产品编号</th>
            <th>产品名称</th>
            <th>购买数量</th>
            <th>产品单价</th>
            <th>产品总价</th>
            <th>操作</th>
        </tr>
        <tr ng-repeat="item in data">
            <td>{{$index+1}}</td>
            <td>{{item.name}}</td>
            <td>
                <button ng-click="decr($index)">-</button>
                <input type="text" ng-model="item.count" readonly="">
                <button ng-click="incr($index)">+</button>
            </td>
            <td>{{item.price}}</td>
            <td>{{item.price*item.count}}</td>
            <td><button ng-click="del($index)">删除</button></td>
        </tr>
        <tr>
            <td colspan="6">总金额<span ng-bind="allSum()|currency:'RMB¥'"></span></td>
        </tr>
        <tr>
            <td colspan="4">总数量<span ng-bind="allNum()"></span></td>
            <td colspan="2"><button ng-click="qingkong()">清空购物车</button></td>
        </tr>
    </table>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值