angular.js实现购物车功能
更新时间:2017年10月23日 10:41:56 作者:飞鸟96
这篇文章主要为大家详细介绍了angular.js购物车功能的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了angular.js购物车功能的具体代码,供大家参考,具体内容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>购物车</title> <script src="angularjs/angular.js"></script> <style> .box{ width: 100%; border-bottom: 1px solid silver; } .box1{ width: 100%; margin-top: 5px; } .box1 button{ width: 100px; height: 40px; background: crimson; color: white; text-align: center; line-height: 40px; float: right; border: 0; border-radius: 13px; } table{ width: 100%; } tr td button{ background: blue; color: white; border: 0; } </style> <script> var my=angular.module("my",[]); my.controller("mys",function ($scope) { /*声明数据对象,初始化商品信息,数据自拟且不低于四条*/ $scope.arr=[ {name:"qq",price:12.9,number:2,state:false}, {name:"wx",price:23.9,number:1,state:false}, {name:"aa",price:99.9,number:1,state:false}, {name:"bb",price:10.9,number:5,state:false} ]; /*删除条目*/ $scope.del=function (index) { if(confirm("确定移除此项嘛?")){ $scope.arr.splice(index,1); } } /*点击”+”按钮输入框中的数量加1,同时可以计算出商品小计,和购物 车总价*/ $scope.jia=function (index) { $scope.arr[index].number++; } /*当点击”-”按钮时输入框中的数量减1,商品小计和总价*/ $scope.jian=function (index) { if($scope.arr[index].number>1){ $scope.arr[index].number--; } else if($scope.arr[index].number==1){ if(confirm("用户是否删除该商品")){ $scope.arr.splice(index,1); } } } /*计算总价格*/ $scope.allSum=function () { var allPrice=0; for(var i=0;i<$scope.arr.length;i++){ allPrice+=$scope.arr[i].price*$scope.arr[i].number; } return allPrice; }; /*清空购物车*/ $scope.alldel=function () { if($scope.arr.length==0){ alert("您的购物车已空"); }else{ $scope.arr=[]; } } /*用户点击第一个checkbox代表全选,全选商品后点击删除选中商品,选中商品被删除, 若购物车商品被全部删除后*/ $scope.allCheck=false; $scope.allx= function () { for(var i=0;i<$scope.arr.length;i++){ if($scope.allCheck==true){ $scope.arr[i].state=true; }else { $scope.arr[i].state=false; } } }; /*每个复选框*/ $scope.itemCheck = function () { var flag = 0; for(var i = 0; i<$scope.arr.length; i++){ if($scope.arr[i].state == true){ flag ++; } } if(flag == $scope.arr.length){ $scope.allCheck = true; }else{ $scope.allCheck = false; } }; /*批量删除*/ $scope.pi=function () { for(var i=0;i<$scope.arr.length;i++){ if($scope.arr[i].state==true){ $scope.arr.splice(i,1); i--; $scope.allCheck = false; } } } }); </script> </head> <body ng-app="my" ng-controller="mys"> <div class="box"> <h2>我的购物车</h2> </div> <div class="box1"> <button ng-click="alldel()" style="margin-right: 10px">清空购物车</button><button ng-click="pi()" style="margin-left: 5px">批量删除</button> </div> <div class="box2"> <table border="1"> <tr> <th><input type="checkbox" ng-model="allCheck" ng-click="allx()"/></th> <th>name</th> <th>price</th> <th>number</th> <th>totalPrice</th> <th>option</th> </tr> <!--用ng-repaet指令将对象遍历并渲染到页面中--> <tr ng-repeat="item in arr"> <td><input type="checkbox" ng-model="item.state" ng-click="itemCheck()"/></td> <td>{{item.name}}</td> <td>{{item.price | currency:"¥:"}}</td> <td><button ng-click="jian($index)">-</button> <input type="text" value="{{item.number}}" style="width: 30px;padding-left: 20px"/> <button ng-click="jia($index)">+</button> </td> <td>{{item.price*item.number | currency:"¥:"}}</td> <td><button ng-click="del($index)">删除</button></td> </tr> <tr> <td colspan="6">总金额<span ng-bind="allSum()|currency:'¥:'"></span></td> </tr> </table> </div> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Angular中封装fancyBox(图片预览)遇到问题小结
这篇文章主要介绍了Angular中封装fancyBox(图片预览)遇到的问题小结,需要的朋友可以参考下2017-09-09详解为Angular.js内置$http服务添加拦截器的方法
所谓拦截器就是在目标达到目的地之前对其进行处理以便处理结果更加符合我们的预期。Angular的$http拦截器是通过$httpProvider.interceptors数组定义的一组拦截器,每个拦截器都是实现了某些特定方法的Factory。本文就介绍了为Angular.js内置$http服务添加拦截器的方法。2016-12-12AngularJS 文件上传控件 ng-file-upload详解
这篇文章主要介绍了AngularJS 文件上传控件 ng-file-upload详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-01-01Angularjs在初始化未完毕时出现闪烁问题的解决方法分析
这篇文章主要介绍了Angularjs在初始化未完毕时出现闪烁问题的解决方法,结合实例形式分析了3种常用的闪烁问题解决方法,需要的朋友可以参考下2016-08-08Ionic + Angular.js实现验证码倒计时功能的方法
验证码倒计时这个功能相信对大家每个人来说都不陌生,之前介绍了在Android中的实现方法,下面这篇文章主要给大家介绍了利用Ionic + Angular.js实现验证码倒计时功能的相关资料,文中介绍的非常详细,需要的朋友们下面来一起看看吧。2017-06-06利用require.js与angular搭建spa应用的方法实例
这篇文章主要给大家介绍了关于利用require.js与angular搭建spa应用的方法实例,文中通过示例代码给大家介绍的非常详细,对大家的理解和学习具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。2017-07-07
最新评论