angular学习笔记(十四)-$watch(3)
同样的例子,还可以这样写:
<!DOCTYPE html>
<html ng-app>
<head>
<title>11.3$watch监控数据变化</title>
<meta charset="utf-8">
<script src="../angular.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="CartController">
<h1>your shopping cart</h1>
<table>
<tr ng-repeat="item in items">
<td>{{item.title}}</td>
<td><input ng-model="item.quantity"/></td>
<td>{{item.price|currency}}</td>
<td class="red">{{item.price*item.quantity|currency}}</td>
<td><button ng-click="remove($index)">remove</button></td>
</tr>
</table>
<hr>
<table>
<tr>
<td>总价: <span class="del">{{computeTotal()|currency}}</span></td>
</tr>
<tr>
<td>折扣: <span class="red">{{discount|currency}}</span></td>
</tr>
<tr>
<td>现价: <span class="green">{{computeNow()|currency}}</span></td>
</tr>
</table>
</div>
</body>
</html>
function CartController ($scope) {
$scope.items = [
{"title":"兔子","quantity":1,"price":"100"},
{"title":"喵","quantity":2,"price":"200"},
{"title":"狗只","quantity":1,"price":"400"},
{"title":"仓鼠","quantity":1,"price":"300"}
];
$scope.remove = function(index){
$scope.items.splice(index,1)
};
$scope.discount = 0;
$scope.computeTotal = function(){
var total = 0;
for(var i=0; i<$scope.items.length; i++){
total += $scope.items[i].quantity*$scope.items[i].price;
}
return total
};
$scope.computeDiscount = function(newV,oldV,scope){
$scope.discount = newV >= 500 ? newV*0.1 : 0 ;
};
$scope.computeNow = function(){
return $scope.computeTotal() - $scope.discount;
};
$scope.$watch('computeTotal()',$scope.computeDiscount);
}
/*
最后这句橙色的,也可以写成:
$scope.$watch($scope.computeTotal,$scope.computeDiscount)
效果一致
*/
1. 视图的总价部分,改成computeTotal()
2. $watch监测computeTotal函数返回值的变化
3. 总价变化,则调用computeDiscount函数计算折扣,其中第一个参数就是最新的总价
4. 视图的现价部分,改成computeNow(),通过总价和折扣计算现价
使用这种方法,逻辑上不够清楚,并且,$scope.computeTotal会被多次执行,影响性能,仅作参考.
-----------------------------------------------------------------------------------------------------------------------
遗留问题:
使用angular实现同一个功能,有多种设计方法,需要考虑它的性能,考虑逻辑性.
目前来说,首先要做到的是能够以清楚的逻辑将程序设计出来,将来再慢慢考虑性能.
angular学习笔记(十四)-$watch(3)的更多相关文章
- 【转】angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- angular学习笔记(十四)-$watch(2)
下面来看一个$watch的比较复杂的例子: 还是回到http://www.cnblogs.com/liulangmao/p/3700919.html一开始讲的购物车例子, 给它添加一个计算总价和折扣的 ...
- angular学习笔记(十四)-$watch(4)
如果需要同时监测多个属性或者对象,并且执行的是同样的回调,可以有两种选择: 1. 监测这些属性连接起来之后的值: $scope.$watch('objOne.a+objTwo.b+...', watc ...
- angular学习笔记(十五)-module里的'服务'
本篇介绍angular中的模块:module 在笔记(二)http://www.cnblogs.com/liulangmao/p/3711047.html里已经讲到过模块,这篇主要讲模块的 '服务' ...
- angular学习笔记(十九)-指令修改dom
本篇主要介绍angular使用指令修改DOM: 使用angular指令可以自己扩展html语法,还可以做很多自定义的事情.在后面会专门讲解这一块的知识,这一篇只是起到了解入门的作用. 与控制器,过滤器 ...
- angular学习笔记(十六) -- 过滤器(2)
本篇主要介绍angular自定义的过滤器: 直接看例子: <!DOCTYPE html> <html ng-app="MyFilter"> <head ...
- angular学习笔记(十六) -- 过滤器(1)
本篇主要介绍过滤器的基本用法: 过滤器用来对数据进行格式的转换,数据格式的转化与逻辑无关,因此,我们使用过滤器来进行这些操作: {{... | filter2: 参数1,参数2... }} expre ...
- angular学习笔记(十)-src和href处理
本篇主要介绍angular中图片的src和链接的href的处理: 用到了以下两个属性: ng-src: 绑定了数据的路径表达式 ng-href: 绑定了数据的路径表达式 例如: <!DOCTYP ...
随机推荐
- 本地文件夹变远程仓库并且提交Github
//初始化本地仓库 $ git init Initialized empty Git repository in C:/Users/root/Desktop/vue-music/.git/ root@ ...
- Mapreduce实例-Top Key
1 public class TopK extends Configured implements Tool { public static class TopKMapper extends Mapp ...
- poj 1879 Truck History
本题链接:点击打开链接 题目大意: 输入n表示卡车辆数,输入每辆卡车编号.即长度为7的字符串,每辆卡车编号均可由其他类型编号衍生过来,求由当中一辆衍生出其他全部的最小衍生次数(有一个字符不同就需衍生一 ...
- iOS CGRectGetMaxX/Y 使用
在iOS的界面布局中我们能够使用CGRectGetMaxX 这种方法来方便的获取当前控件的x坐标值+宽度的数值.这样便能够方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...
- LeetCode 37 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- 【基于Android的ARM汇编语言系列】之三:ARM汇编语言程序结构
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell [ ...
- 〖Linux〗Shell脚本修改输出文字颜色
Shell函数: echocolor(){ color=${} && shift case ${color} in black) echo -e "\e[0;30m${@}\ ...
- servlet 服务器HTTP请求头说明
Accept:用于告诉服务器,客户机支持的数据类型. Accept-Charset:用于告诉服务器,客户机采用的编码. Accept-Language:客户机的语言环境. Host:客户机通过该头告诉 ...
- Java实现可视化迷宫
代码地址如下:http://www.demodashi.com/demo/14547.html 需求 使用深度优先算法求解迷宫路径,使用Java实现求解过程的可视化,可单步运行,形象直观. 演示效果 ...
- 老毛桃pe装机工具备份系统
电脑故障可以说是难以避免的,误操作或者修改了哪个设置系统就莫名其妙崩溃了.这在日常使用当中并不鲜见,许多用户就会寻求备份系统方法.有没有好的一键备份系统教程可以参考呢?在本篇教程中,就容我跟大家讲讲怎 ...