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)-购物车增删改查练习的更多相关文章

  1. hibernate系列笔记(1)---Hibernate增删改查

    Hibernate增删改查 1.首先我们要知道什么是Hibernate Hibernate是一个轻量级的ORMapping对象.主要用来实现Java和数据库表之间的映射,除此之外还提供数据查询和数据获 ...

  2. EF学习笔记-1 EF增删改查

    首次接触Entity FrameWork,就感觉非常棒.它节省了我们以前写SQL语句的过程,同时也让我们更加的理解面向对象的编程思想.最近学习了EF的增删改查的过程,下面给大家分享使用EF对增删改查时 ...

  3. day5笔记 列表 list 增删改查

    列表的使用 一.索引和切片 # 索引和切片,用法与字符串一样 l = [1,2,3,'af','re',4,'45'] print(l[0]) print(l[3]) print(l[-1]) # ' ...

  4. 【JAVAWEB学习笔记】20_增删改查

    今天主要是利用三层架构操作数据库进行增删查改操作. 主要是编写代码为主. 附图: 前台和后台 商品的展示 修改商品

  5. HTML5+ 学习笔记3 storage.增删改查

    //插入N条数据 function setItemFun( id ) { //循环插入100调数据 var dataNum = new Number(id); for ( var i=0; i< ...

  6. Python学习笔记-列表的增删改查

  7. [学习笔记] Oracle基础增删改查用法

    查询 select *|列名|表达式 from 表名 where 条件 order by 列名 select t.* from STUDENT.STUINFO t where t.stuname = ...

  8. Mysql学习笔记(三)对表数据的增删改查。

    正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...

  9. MySQL数据库学习笔记(十二)----开源工具DbUtils的使用(数据库的增删改查)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

随机推荐

  1. Java基础——类加载机制

    什么叫类加载 JVM把 .class 字节码文件加载到内存,并进行相关的校验.解析.初始化,最终转换为虚拟机可用的JAVA类型的过程,称为JVM类加载机制. (当然,JVM并不关心class文件的来源 ...

  2. QtGUI Module's Classes

    Qt GUI C++ Classes The Qt GUI module provides the basic enablers for graphical applications written ...

  3. Java并发工具类(三):控制并发线程数的Semaphore

    作用 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源. 简介 Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数 ...

  4. 一行 Python 代码能干嘛?

    Python 有很多优雅有趣的代码写法,同时还很简短,以至于当我刚开始接触这个编程语言的时候,就爱不释手.而前几天的编程语言榜单中 Python 也超越了 Java 成为了第一,挺替 Python 开 ...

  5. Eclipse启动Tomcat错误:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already

    Eclipse启动Tomcat错误: Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are alread ...

  6. fortran shapefile学习

    试图编写一个fortran程序,用以判断给定的点是否落在给定shapefile的范围内. 需要利用到FortranGIS库 ,而该库又依赖于Shapefile C Library 安装shapelib ...

  7. myeclipse激活后server不能用问题

    一般是由于激活失败造成的,这种问题就卸了重新安装吧,目前还没有找到合理的方法解决,这个还真的看哥们的运气了,我是装了不下5遍才激活成功的,一般情况下,在激活的时候 出现下图的情况,Usercode写好 ...

  8. 宝塔中mysql数据库命名小坑

    今天在通过宝塔新建网站,添加mysql数据库,名字中间有下划线,发现能够创建成功,但是实际链接后,是没有这个数据库的.是宝塔的原因还是liunx服务器的原因? 不支持下划线的数据库名字吗? 比如 bo ...

  9. MAVEN项目导入src/test/java项目报错

    转载博客:https://blog.csdn.net/gengjianchun/article/details/78679036    https://blog.csdn.net/jsloveyou/ ...

  10. GitHub笔记(四)——标签管理

    五 标签管理 1 打标签.默认master $ git tag v1.0 要对add merge这次提交打标签,它对应的commit id是f52c633,敲入命令: $ git tag v0.9 f ...