AangularJS运用bootstrap.js结合创建购物表单

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

    <link rel="stylesheet" type="text/css"  href="css/bootstrap.min.css">

//导入这三个js使用

    <script src="jss/jquery-3.2.1.js"></script>
    <script src="jss/bootstrap.min.js"></script>
    <script src="jss/angular1.4.6.min.js"></script>
    <style>
        span{padding-left:50px;padding-right:50px;}
    </style>

    <script>

//angular 在最跟处创建ng-app 和controller 相当于确定控制的入口

        var app=angular.module('myApp',[]);

        app.controller('myCtrl',function($scope){

//创建要显示的集合

            $scope.shopList=[
                { name:'显卡',price:'80.90',num:10},
                { name:'读卡器',price:'20.40',num:10},
                { name:'键盘',price:'46.90',num:10},
                { name:'显示器',price:'231.00',num:10},
                { name:'主机',price:'279.30',num:10}
            ];
            //点击对应的button减少数量
            $scope.reduce= function (index) {
                if($scope.shopList[index].num>1){
                    $scope.shopList[index].num--;
                }else{
                    $scope.remove(index);
                }
            };
            //点击对应的button增加数量
            $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;
            };
            //点击移除button   弹出是否移除
            $scope.remove=function(index){
                if(confirm('确定移吗?')){
                    $scope.shopList.splice(index,1);
                }
            };
            //使得输入框中不得小于等于0  让它最小的为1
            $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和ng-controller 这里我就把controller放入下边盒子里了

<body ng-app="myApp">
<div class="container">
    <div ng-controller="myCtrl">
        <ul class="list-group">
            <li ng-repeat="shop in shopList" class="list-group-item">
                <span>{{shop.name}}</span>
                <span>{{shop.price|currency}}</span>
                <span>
                    <button ng-click="reduce($index)">-</button>
                    <input type="text" placeholder="请输入大于0的数" ng-model="shop.num" ng-change="change($index)">
                    <button ng-click="add($index)">+</button>
                </span>
                <span>{{shop.price*shop.num}}</span>
                <button class="btn btn-primary btn-xs" ng-click="remove($index)">移除</button>
            </li>
        </ul>
        总价:<span ng-bind="allSum()"></span>  总数:<span ng-bind="allNum()"></span>
        <button class="btn btn-warning "ng-click="removeAll()">清空购物车</button>
    </div>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值