angular+做一个购物车可以删除,计算总额

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="angular/angular.js"></script>
   <style>
       table{
           width: 500px;
           height: 300px;
           border-collapse: collapse;
           text-align: center;
       }
       td{
           border: 1px solid black;
       }
   </style>
    <script>
        var myapp=angular.module("myapp",[]);
        myapp.controller("myCtrl",function ($scope) {
            $scope.goods=[{
                gname:"iphone8",
                num:"3",
                price:"999"
            },{
                gname: "iphone7",
                num: "4",
                price: "599"
            },{
                gname: "iphone6",
                num: "5",
                price: "499"
            },{
                gname: "iphone5",
                num: "6",
                price: "399"
            }
            ];
            $scope.allSum=function () {
                var allPrice=0;
                for(var i=0;i<$scope.goods.length;i++){
                    allPrice+=$scope.goods[i].price*$scope.goods[i].num;

                }
                return allPrice;
            };
            $scope.remove=function (index) {
                if(confirm('确定移除此项嘛?')){
                    $scope.goods.splice(index,1);
                }
                if($scope.goods.length==0){
                    alter('你的购物车为空');
                }
            };
        })
    </script>
</head>
<body ng-app="myapp" ng-controller="myCtrl">
<table>
    <tr>
        <td colspan="5">你的购物车</td>
        <tr>
    <td>商品名称</td>
    <td>数量</td>
    <td>单价</td>
    <td>小计</td>
    <td>操作</td>
    </tr>
    <tr ng-repeat="arr in goods">
        <td>{{arr.gname}}</td>
        <td>{{arr.num}}</td>
        <td>{{arr.price|currency:"RMB¥"}}</td>
        <td>{{arr.num*arr.price|currency:"RMB¥"}}</td>
        <td><button ng-click="remove($index)">删除</button></td>
    </tr>
    <tr>
        <td colspan="5">总金额<span ng-bind="allSum()|currency:'RMB'"></span></td>
    </tr>
</table>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值