AngularJs 终极购物车(实例讲解)

时间:2021-05-28

废话不多说,直接上代码:

<!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); } } $scope.jia=function (index) { $scope.arr[index].number++; } $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.<span style="color:#660e7a"><strong>arr</strong></span>[<span style="color:#458383">i</span>].<span style="color:#660e7a"><strong>price</strong></span>*$scope.arr[i].number; } return allPrice; }; $scope.alldel=function () { if($scope.arr.length==0){ alert("您的购物车已空"); }else{ $scope.arr=[]; } } $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>

以上这篇AngularJs 终极购物车(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章