(angularjs )购物车案例

上代码:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>购物</title>
    <link rel="stylesheet" href="bootstrap.min.css" />
    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
    <script src="bootstrap.min.js"></script>
    <script src="jquery-2.1.0.js"></script>
    <style>
        span{padding-left:50px;padding-right:60px;}
    </style>
    <script>
        var app=angular.module('myApp',[]);
        app.controller('myCtrl',function($scope){
            //声明数据对象,初始化商品信息
            $scope.shopList=[
                { name:'单片机',price:'80.90',num:1},
                { name:'电烙铁',price:'20.40',num:1},
                { name:'万用表',price:'46.90',num:1},
                { name:'示波器',price:'21.10',num:1},
                { name:'电饭锅',price:'79.30',num:1}
            ];
            //点击减少  
            $scope.reduce= function (index) {
                if($scope.shopList[index].num>1){
                    $scope.shopList[index].num--;
                }else{
                    $scope.remove(index);
                }
            };
            //点击增加  
            $scope.add=function(index){
                $scope.shopList[index].num++;
            };
            //计算总价  
            $scope.allSum=function(){
                var allPrice = 0;
                for(var i= 0;i<$scope.shopList.length;i++){
                    allPrice+=$scope.shopList[i].price*$scope.shopList[i].num;
                }
                return allPrice;
            };
            //计算总数量  
            $scope.allNum=function(){
                var allShu=0;
                for(var i=0;i<$scope.shopList.length;i++){
                    allShu+=$scope.shopList[i].num;
                }
                return allShu;
            };
            //移除一项  
            $scope.remove=function(index){
                if(confirm('您是否将商品移除购物车?')){
                    $scope.shopList.splice(index,1);
                }
            };
            //使得输入框中不得小于等于0  
            $scope.change=function(index){
                if($scope.shopList[index].num>=1){
                }else{
                    $scope.shopList[index].num=1;
                }
            };
            //清空购物车  
            $scope.removeAll=function(){
                if(confirm('确定清空购物车')){
                    $scope.shopList=[];
                }
            }

        });

    </script>
</head>
<body ng-app="myApp">
<div class="container">
    <h1>我的购物车</h1>
    <div ng-controller="myCtrl">
        <button class="btn btn-warning "ng-click="removeAll()" style="background-color:#D9534F; color: white; margin-left:10px;">清空购物车</button>
        <ul class="list-group">
            <li class="list-group-item">
                <span><input type="checkbox" ng-model="select_all"/></span>
                <span>name</span>
                <span>price</span>
                <span>number</span>
                <span>totalPrice</span>
                <span>option</span>
            </li>
            <!--
                
                描述:用ng-repaet指令将对象遍历并渲染到页面中
            -->
            <li ng-repeat="shop in shopList" class="list-group-item">
                <span><input ng-model="select_all" type="checkbox" /></span>
                <span>{{shop.name}}</span>
                <span>{{shop.price|currency : '¥'}}</span>
                <span>  
                    <button ng-click="reduce($index)" style="background-color:#337AB7;">-</button>  
                    <input type="text" placeholder="请输入大于0的数" ng-model="shop.num" ng-change="change($index)" style="width: 30px;">  
                    <button ng-click="add($index)" style="background-color:#337AB7;">+</button>  
                </span>
                <span>{{shop.price*shop.num|currency : '¥'}}</span>
                <button class="btn btn-primary btn-xs" ng-click="remove($index)" style="background-color:#337AB7;">删除</button>
            </li>
            <li class="list-group-item">总价为:¥<span ng-bind="allSum()"></span></li>
        </ul>

    </div>
</div>
</body>
</html>

上图片:


需要注意导包:

<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="jquery-2.1.0.js"></script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值