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 ...
随机推荐
- wepy - 转换成h5
包地址:http://npm.taobao.org/package/wepy-web 1. npm 安装 npm install wepy-web 2.yarn 按照 yarn add wepy-we ...
- mac SVN 设置代理
1. 设置代理一种方法:Finder 菜单里面 -> 前往 -> 前往文件夹 -> /Users/username/.subversion -> 编辑文件 servers 在文 ...
- grid++ 开发教程
grid++ 开发教程 CreateTime--2018年4月24日18:43:23 Author:Marydon 1.插入报表头.明细网格.报表尾 右键-->插入-->报表节--&g ...
- openfire + spark 展示组织机构(客户端)
在spark 加一个插件用于展示组织机构, 参考了好多人的代码 插件主类增加一个 TAB用于展示机构树 package com.salesoa.orgtree; import java.net.URL ...
- 赵雅智_android多线程下载带进度条
progressBar说明 在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度. 一个进度条也可不确定其进度.在不确定模式下, ...
- 推荐系统之 BPR 算法及 Librec的BPR算法实现【1】
[推荐系统之 BPR 算法] 1.关于BPR的论文原文: BPR: Bayesian Personalized Ranking from Implicit Feedback 2.参考1:论文快读 - ...
- 【LeetCode】88. Merge Sorted Array (2 solutions)
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- eclipse3.3插件更新攻略
eclipse有种在线(需有网络)安装插件方法,随着eclipse版本的不同,UI会有所改变.这里记录下e3.3的安装方法 1.选择Find and Install(查找并且安装)选项 ...
- POJ 3691 DNA repair (DP+AC自动机)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4815 Accepted: 2237 Descri ...
- ASP.NET中UrlEncode应该用Uri.EscapeDataString()
今天,茄子_2008反馈他博客中的“C++”标签失效.检查了一下代码,生成链接时用的是HttpUtility.UrlEncode(url),从链接地址获取标签时用的是HttpUtility.UrlDe ...