使用AngularJS实现一个简单的购物车,主要感受强大的双向绑定和只关注对象不关注界面特性。
先看看界面:
点击+-操作和删除:
这些全部只需要操作数据源就行,不需要关注界面。
实现过程:
一、使用任何语言创建一个服务端:
public class ShoppingCar { public string Title { get; set; } public decimal UnitPrice { get; set; } public int Count { get; set; } }
public ActionResult GetCar() { List<ShoppingCar> cars = new List<ShoppingCar> { new ShoppingCar { Title="苹果",Count=1,UnitPrice=2.5m}, new ShoppingCar { Title="香蕉",Count=3,UnitPrice=1.5m}, new ShoppingCar { Title="苦瓜",Count=1,UnitPrice=3.5m}, new ShoppingCar { Title="黄瓜",Count=3,UnitPrice=2.2m} }; return Json(cars,JsonRequestBehavior.AllowGet); } public ActionResult AddCar(List<ShoppingCar> car) { return Json("ok", JsonRequestBehavior.AllowGet); }
二、前台实现:
<div ng-app="DemoApp" ng-controller='CartController'> <table class="table table-striped"> <thead> <tr> <td>标题</td> <td>单价</td> <td>数量</td> <td>小计</td> <td>删除</td> </tr> </thead> <tbody> <tr ng-repeat="item in ShoppingCar"> <td>{{item.Title}}</td> <td>{{item.UnitPrice}}</td> <td> <input type="text" ng-cloak ng-model="item.Count" style="width:50px;text-align:center;"> <button my-adds ng-click="UpdateCar(item.Title,1)" ng-class="{cursors:true}">+</button> <button my-minus ng-click="UpdateCar(item.Title,-1)" ng-class="{cursors:true}">-</button> </td> <td>{{item.Count*item.UnitPrice | number:2}}</td> <td><button my-minus ng-click="UpdateCar(item.Title,-100)" ng-class="{cursors:true}">删</button></td> </tr> </tbody> </table> <p ng-init=0>总价格:{{ total | number:2}}</p> <button type="button" ng-click="submit()">提交</button> </div>
三、Angular部分
var app = angular.module('DemoApp', []); app.controller('CartController', ['$scope', '$http', function ($scope, $http) { $scope.ShoppingCar = {} var GetCar = function () { $http.get('/Employee/GetCar') .success(function (response) { $scope.ShoppingCar = response; GetTotal(); }); } $scope.total = 0; var GetTotal = function () { for (var i = 0; i < $scope.ShoppingCar.length; i++) { var item = $scope.ShoppingCar[i]; $scope.total += item.Count * item.UnitPrice; } } $scope.UpdateCar = function (title, count) { for (var i = 0; i < $scope.ShoppingCar.length; i++) { var item = $scope.ShoppingCar[i]; if (item.Title == title) { item.Count = item.Count + count;//这里可以增加上下限制 if (item.Count < 0) { $scope.ShoppingCar.splice(i, 1); } } } GetTotal(); } $scope.submit = function () { $http.post('/Employee/AddCar', $scope.ShoppingCar) .success(function (response) { alert(response); }); } GetCar(); }]);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“Angular实现购物车计算示例代码”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
2024年05月09日
2024年05月09日
- 黄征-爱情诺曼底-2003-APE整轨
- 妥协只是暂时的!《对马岛之魂》PC版的多人模式仍需绑定PSN
- 开启抢先体验后 《盗贼之海》成PS美国第二畅销游戏
- 《恶意不息》老板再用中文询问中国玩家:对汉化是否满意
- 2个女生.2000-坚固友情精选辑【EMI百代】【WAV+CUE】
- 潘越云李丽芬吴楚楚.1981-三人展(滚石民歌巨星系列)【滚石】【WAV+CUE】.
- 元若蓝.2006-爱无限大【滚石】【WAV+CUE】
- 刘德华《经典传说·粤语精选集》[WAV/分轨][610MB]
- 庄心妍《2020年新专辑歌曲 念》[WAV/分轨][400MB]
- 群星《音色精美绝伦 粤语经典 2CD》[WAV+CUE][1.4GB]
- 欢乐钓鱼大师不充钱怎么玩 欢乐钓鱼大师不充钱搞鱼竿玩法介绍
- 欢乐钓鱼大师隐藏奖励有什么 欢乐钓鱼大师隐藏奖励介绍
- 欢乐钓鱼大师橙色鱼竿哪个好 欢乐钓鱼大师橙色鱼竿选择攻略
- 《最后的克劳迪亚》基础攻略 简述+配置+下载
- 《下一站江湖2》5月3日更新内容介绍