angularJS1笔记-(3)-购物车增删改查练习
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="../../vendor/bootstrap3/css/bootstrap.min.css">
</head>
<body ng-app="myApp">
<div class="container">
<table class="table" ng-controller="firstController" ng-show="cart.length">
<thead>
<tr>
<th>产品编号</th>
<th>产品名字</th>
<th>购买数量</th>
<th>产品单价</th>
<th>产品总价</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in cart">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>
<button type="button" ng-click="reduce(item.id)" class="btn btn-primary">-</button>
<input type="text" value="item.quantity" ng-model="item.quantity"/>
<button type="button" ng-click="add(item.id)" class="btn btn-primary">+</button>
</td>
<td>{{item.price}}</td>
<td>{{item.price*item.quantity}}</td>
<td>
<button type="button" class="btn btn-primary" ng-click="remove(item.id)">移除</button>
</td>
</tr>
<tr>
<td>
总价:
</td>
<td>
{{totalPrice()}}
</td>
<td>
总购买数量:
</td>
<td>
{{totalCount()}}
</td>
<td colspan="2">
<button type="button" class="btn btn-primary" ng-click="cart = {}">清空购物车</button>
</td>
</tr>
</tbody>
</table>
<p ng-show="!cart.length">您的购物车为空</p>
</div>
<script type="text/javascript" src="../../vendor/angular/angularJs.js"></script>
<script type="text/javascript" src="app/index.js"></script>
</body>
</html>
js:
angular.module('myApp', []).controller('firstController', function ($scope) {
$scope.cart = [
{
id: 1000,
name: 'iphone7p',
quantity: 2,
price: 4999
},
{
id: 1001,
name: 'iphone4p',
quantity: 2,
price: 999
},
{
id: 1002,
name: 'iphone6p',
quantity: 6,
price: 3999
},
{
id: 1003,
name: 'iphoneSE',
quantity: 20,
price: 11999
}
];
//计算总价
$scope.totalPrice = function () {
var totalPrice = 0;
angular.forEach($scope.cart, function (item) {
totalPrice += parseInt(item.quantity) * item.price;
})
return totalPrice;
}
//计算总购买数量
$scope.totalCount = function () {
var totalCount = 0;
angular.forEach($scope.cart, function (item) {
totalCount += parseInt(item.quantity);
})
return totalCount;
}
//移除
$scope.remove = function (id) {
var index = -1;
angular.forEach($scope.cart, function (item, key) {
if (item.id == id) {
index = key;
}
})
if (index != -1) {
$scope.cart.splice(index, 1);
}
}
//找索引 因为angular的机制是通过索引来删除
var findIndex = function (id) {
var index = -1;
angular.forEach($scope.cart, function (item, key) {
if (item.id == id) {
index = key;
return;
}
});
return index;
}
//添加
$scope.add = function (id) {
var index = findIndex(id);
if (index != -1) {
++$scope.cart[index].quantity;
}
}
//删除
$scope.reduce = function (id) {
var index = findIndex(id);
if (index != -1) {
var item = $scope.cart[index];
if(item.quantity>1){
--item.quantity;
}else {
var returnKey = confirm('从购物车中删除该商品吗?')
if (returnKey) {
$scope.cart.splice(index, 1);
}
}
}
}
$scope.$watch('cart',function (newValue,oldValue) {
angular.forEach(newValue,function (item, key) {
if(item.quantity<1){
var returnKey = confirm('是否从购物车内删除该产品?');
if(returnKey){
$scope.cart.splice(key, 1);
}else{
item.quantity = oldValue[key].quantity;
}
}
})
},true);
});
最终结果:

因为angular是MVVM模式的双向绑定数据,所以当你改变其中变量的时候其他地方凡是用到了此变量地方都会跟着变。
angularJS1笔记-(3)-购物车增删改查练习的更多相关文章
- hibernate系列笔记(1)---Hibernate增删改查
Hibernate增删改查 1.首先我们要知道什么是Hibernate Hibernate是一个轻量级的ORMapping对象.主要用来实现Java和数据库表之间的映射,除此之外还提供数据查询和数据获 ...
- EF学习笔记-1 EF增删改查
首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...
- day5笔记 列表 list 增删改查
列表的使用 一.索引和切片 # 索引和切片,用法与字符串一样 l = [1,2,3,'af','re',4,'45'] print(l[0]) print(l[3]) print(l[-1]) # ' ...
- 【JAVAWEB学习笔记】20_增删改查
今天主要是利用三层架构操作数据库进行增删查改操作. 主要是编写代码为主. 附图: 前台和后台 商品的展示 修改商品
- HTML5+ 学习笔记3 storage.增删改查
//插入N条数据 function setItemFun( id ) { //循环插入100调数据 var dataNum = new Number(id); for ( var i=0; i< ...
- Python学习笔记-列表的增删改查
- [学习笔记] Oracle基础增删改查用法
查询 select *|列名|表达式 from 表名 where 条件 order by 列名 select t.* from STUDENT.STUINFO t where t.stuname = ...
- Mysql学习笔记(三)对表数据的增删改查。
正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...
- MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
随机推荐
- struts2第四天——拦截器和标签库
一.拦截器(interceptor)概述 struts2是个框架,里面封装了很多功能,封装的很多功能都是在拦截器里面. (属性封装.模型驱动等都是封装在拦截器里面) struts2里面封装了很多功能, ...
- Codeforces VK Cup 2018 Div.2
总题面传送门 这次考试只过了3题,前三题题目难度并不大,但是第三题的代码细节卡了我两个半小时(基本上整场考试),所以以后要合理把握时间,注意把握代码细节,并更加完善我的代码风格,使其更加简练.(赛外话 ...
- 【python3】将视频转换为代码视频
一.环境准备 1.需要安装opencv,直接安装 pip install opencv-python 2.需要安装ffmpeg ,直接解压免安装,下载传送门: 将 ffmpeg.exe 的路径复制,替 ...
- c++ 自定义类型,函数指针类型
用typedef定义函数指针类型 -函数指针和函数指针数组 46课里边有如下代码 int add(int a,int b,int d) { return a+b+d; } int mul(int a, ...
- Linux下开发python django程序(Form表单对象创建和使用)
1.在setting.py文件中修改节点,注释掉其中一行 MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'dj ...
- 【转载】MFC动态创建控件及其消息响应函数
原文:http://blog.sina.com.cn/s/blog_4a08244901014ok1.html 这几天专门调研了一下MFC中如何动态创建控件及其消息响应函数. 参考帖子如下: (1)h ...
- windows7 64位机上配置支持GPU版(CUDA7.5)的OpenCV2.4.13操作步骤
很久之前在windows7 32位上配置过GPU版的opencv,可参考http://blog.csdn.net/fengbingchun/article/details/9831837 Window ...
- imu标定 imu_tk
1. 首先标定加速度计,这是imu加速度计xyz三个轴在标定过程中的读数: 标定结果: 2. 利用加速度计的标定结果,标定陀螺仪,结果: 也可以使用港科大开源的一个工具: https://github ...
- lastIndexOf()
方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索.
- 传输控制协议--- Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP) 用于网络通信的传输控制和网络协议套件,包括很多协议,其中最主要的是TCP和IP协议.TCP/IP属于UNIX类系统的内置协议,被 ...