angularJS商品购物车案例

<!DOCTYPE html>
<html ng-app="shopping">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="bootstrap-3.3.4-dist/css/bootstrap.css"/>
</head>
<body>
    <div class="container">
     <div ng-controller="goodsList">
        <table class="table" ng-show="goods.length">
            <thead>
             <tr>
                <th>
                    产品编号
                </th>
                <th>
                    产品名字
                </th>
                <th>
                    购买数量
                </th>
                <th>
                    产品单价
                </th>
                <th>
                    产品总价
                </th>
                <th>
                    操作
                </th>
             </tr>
            </thead>
            <tbody>
                <tr ng-repeat="item in goods">
                    <td><span ng-bind="item.id"></span></td>
                    <td><span ng-bind="item.name"></span></td>
                    <td>
                        <input type="button" value="-" class="btn" ng-click="subNumber(item.id)"/>
                        <input type="text" class="text-center" ng-model="item.sum"/>
                        <input type="button" value="+" class="btn" ng-click="addNumber(item.id)"/>
                    </td>
                    <td><span ng-bind="item.unitPrice"></span></td>
                    <td><span ng-bind="item.sum * item.unitPrice"></span></td>
                    <td><input type="button" value="移除" class="btn btn-danger" ng-click="remove(item.id)"/></td>
                </tr>
                <tr>
                    <td colspan="2">购买总价:<span ng-bind="sumPricesFn()"></span></td>
                    <td colspan="2">购买总数:<span ng-bind="sumFn()"></span></td>
                    <td colspan="2"><input type="button" value="清空购物车" class="btn btn-danger" ng-click="clear()"/></td>
                </tr>
            </tbody>
        </table>
        <div ng-show="!goods.length">您的购物车为空!</div>
     </div>
    </div>
</body>
<script src="../angular.js"></script>
<script>
    var app=angular.module("shopping",[]);
    app.controller("goodsList", function($scope){
        $scope.goods=[
            {
                id:1,
                name:‘苹果6Plus‘,
                sum:3,
                unitPrice:5000
            },
            {
                id:2,
                name:‘苹果5s‘,
                sum:1,
                unitPrice:4000
            },
            {
                id:3,
                name:‘lenovo P404‘,
                sum:1,
                unitPrice:3899
            },
            {
                id:4,
                name:‘飞科剃须刀f77‘,
                sum:10,
                unitPrice:100
            },
            {
                id:5,
                name:‘情侣体恤‘,
                sum:2,
                unitPrice:110
            }
        ];
        //单件商品总价
        $scope.sumPricesFn = function(){
            var sumPrices = 0;
            angular.forEach($scope.goods,function(item){
                sumPrices += item.sum * item.unitPrice;
            });
            return sumPrices;
        };
        //单件商品总数
        $scope.sumFn = function(){
            var sum = 0;
            angular.forEach($scope.goods,function(item){
                sum += Number(item.sum);
            });
            return sum;
        };
        var getIndex = function(id){
            var stateIndex = -1;
            angular.forEach($scope.goods, function(item, index){
                if(id === item.id){
                    stateIndex = index;
                }
            });
            return stateIndex;
        }
        //移除方法
        $scope.remove = function(id){
            var tmpGoodIndex = getIndex(id);
            if(tmpGoodIndex !== -1){
                //将从索引位置为stateIndex删除一个值
                $scope.goods.splice(tmpGoodIndex, 1);
            }
        };
        //清空购物车方法
        $scope.clear = function(){
            $scope.goods=[];
        }
        //增加商品数量
        $scope.addNumber = function(id){
            var tmpGoodIndex = getIndex(id);
            if(tmpGoodIndex !== -1){
                ++$scope.goods[tmpGoodIndex].sum;
            }
        };
        //减少商品数量
        $scope.subNumber = function(id){
            var tmpGoodIndex = getIndex(id);
            if(tmpGoodIndex !== -1){
                --$scope.goods[tmpGoodIndex].sum;
            }
        };

        $scope.$watch(‘goods‘, function(newVal, oldVal){
            angular.forEach(newVal,function(item, index){
                //监听商品信息如果为非法字符那么回归原来的商品数量
                if(isNaN(item.sum) || item.sum < 0){
                    item.sum = oldVal[index].sum;
                }
            });
        },true);
    });
</script>
</html>
时间: 2024-10-19 09:53:08

angularJS商品购物车案例的相关文章

easyUI拖动购物车案例

<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="keywords" content="jquery,ui,easy,easyui,web"> <title>拖动购物车案例</title

AngularJS路由使用案例

AngularJS路由使用案例: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>AngularJS路由使用案例</title> 6 <script type="text/javascript" src="../js/angular.min.js"></script&

DOM操作相关案例 模态对话框,简易留言板,js模拟选择器hover,tab选项卡,购物车案例

1.模态框案例 需求: 打开网页时有一个普通的按钮,点击当前按钮显示一个背景图,中心并弹出一个弹出框,点击X的时候会关闭当前的模态框 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ padding: 0; margin: 0; }

angularjs制作购物车功能

尊重劳动成果,转载请注明出处(http://blog.csdn.net/sllailcp/article/details/47833315)... 初学angularJS   闲暇之余做了个小案例. 功能:计算购物车商品的价格,以及删除购物车商品. 以下是完整案例(jQuery和angularjs需要自己引入) <!doctype html> <html ng-app="myApp"> <head> <meta charset="ut

购物车案例 session

package com.cn.session; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletR

angularjs经典小案例

<script src="js/angular.min.js"></script>     //链接angularjs文件 <div ng-app> //注意此处ng-app不可以忘记噢,也可以加给html或者body      <label>Name</label><br/>      <input type="text" ng-model="name" placeh

查找商品信息案例

-----------------------------------------------------------------------------------------jsp------------------------------------------------------------------------------------------------------------ <title>查看所有商品</title><script type="

python 商品购物车

1 Shopping_Cart=[] 2 3 commodity=[("iphone",5000), 4 ("bike",200), 5 ("book",100), 6 ("computer",3000), 7 ("car",10000), 8 ] 9 salary=input("please input your salary:") 10 11 # print(commodity) 1

vue界面初始化查询列表的方法之购物车案例

var vm = new Vue({ el:"#app", data:{ totalMoney:0, productList:[], checkAllFlag:false , delFlag:false delOneProduct:[], }, filters:{ //局部过滤器 ,调用方法{{number | formatMoney}} formatMoney:function(value){ return "$" +value.toFixed(2) //留两位小