标签:cti enter dig called eval title each maximum size
<div ng-controller="ctrl"> <h2>$watch</h2> <div> <input type="text" ng-model="value1"/> </div> <div ng-bind="w1"></div> <h2>$watchGroup</h2> <div> <input type="text" ng-model="value2"/> <input type="text" ng-model="value3"/> </div> <div ng-bind="w2"></div> <h2>$watchCollection</h2> <ul> <li ng-repeat="v in arr" ng-bind="v"></li> </ul> <div ng-bind="w3"></div> </div>
angular.module(‘nickApp‘, []) .controller("ctrl", ["$scope", "$timeout", function ($scope, $timeout) { // $watch var watcher = $scope.$watch("value1", function (newVal, oldVal) { $scope.w1 = "$watch--" + "new:" + newVal + ";" + "old:" + oldVal; if (newVal == ‘clear‘) {//设置一个注销监听的条件 watcher(); //注销监听 } }); // $watchGroup $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) { //注意:newVal与oldVal都返回的是一个数组 $scope.w2 = "$watchGroup--" + "new:" + newVal + ";" + "old:" + oldVal; }); // $watchCollection $scope.arr = [‘nick‘, ‘ljy‘, ‘ljj‘, ‘zhw‘]; $scope.$watchCollection(‘arr‘, function (newVal, oldVal) { $scope.w3 = "$watchCollection--" + "new:" + newVal + ";" + "old:" + oldVal; }); $timeout(function () { $scope.arr = [‘my‘, ‘name‘, ‘is‘, ‘nick‘]; }, 2000); }])
<h2>小案例</h2> <ul> <li ng-repeat="item in items.goodsArr"> <p ng-bind="item.goods"></p> <p> <span>单价:</span> <span ng-bind="item.price"></span> </p> <div> <input type="number" ng-model="item.num"> <span>个</span> </div> </li> </ul> <div> <span>总计:</span> <span ng-bind="items.sum"></span> <span>元</span> </div>
// 小案例 .factory(‘watchService‘, [function () { var items = { goodsArr: [{ goods: ‘goods1‘, price: 10, num: ‘‘ }, { goods: ‘goods2‘, price: 20, num: ‘‘ }], sum: 0 }; return { getItemsSave: function () { return items; } }; }]) .controller(‘bodyCtl‘, [‘$scope‘, ‘watchService‘, function ($scope, watchService) { $scope.items = watchService.getItemsSave(); // 这里要监听数量变化计算综合 //一 只监听所有num变化计算总额 var watchArr = []; $scope.items.goodsArr.forEach(function (v, i) { watchArr.push("items.goodsArr[" + i + "][‘num‘]"); }); $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal与oldVal都返回的是一个数组 $scope.items.sum = 0; $scope.items.goodsArr.forEach(function (v, i) { $scope.items.sum += v.price * (v.num > 0 ? v.num : 0); }); }); /* //二 这样写则监听items.goodsArr所有成员 $scope.$watch(‘items.goodsArr‘, function () { $scope.items.sum = 0; $scope.items.goodsArr.forEach(function (v, i) { $scope.items.sum += v.price * (v.num > 0 ? v.num : 0); }); }, true);*/ }])
全部代码
<!DOCTYPE html> <html ng-app="nickApp"> <head> <meta charset="UTF-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>angular之$watch、$watchGroup、$watchCollection</title> <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> <script> /* * 1,原型:$watch: function(watchExp, listener, objectEquality, prettyPrintExpression){}; * 2,参数:watchExp(必须):{(function()|string)},可以字符串表达式,也可以带当前scope为参数的函数 * - `string`: Evaluated as {@link guide/expression expression} * - `function(scope)`: called with current `scope` as a parameter. * 3,参数:listener(必须):function(newVal, oldVal, scope),观察的表达式变化的时候调用的函数。 * 4,参数:objectEquality(非必须):是否监视个对象,默认为false * 5,$scope.$digest().会执行所有的同$scope下的$watch。 * 但会出错$apply already in progress,换了$rootScope也一样。 * 原因-参考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643 * $digest、$apply、$$phase这些属性或者方法其实都是$scope中的私有的,最好不要使用。 * 6,$watch一个对象。 * 如果要监视对象的变化(地址改变),$watch对象名,第三个参数默认; * 如果监测对象中某一属性,可以写user.name的形式,第三个参数默认; * 如果监测对象中全部属性,$watch对象名,第三个参数true; * 7,$watchGroup,第一个参数是一个表达式的数组或者返回表达式的数组的函数。 * 8,$watchCollection; * js中数组也是对象,但按照$watch一个对象的方式,只有数组引用变了才能监听变化,增加删除$watch监听不到,所以就有了$watchCollection。 * function(obj, listener):第一个参数必须对象或者返回对象的函数。 */ angular.module(‘nickApp‘, []) .controller("ctrl", ["$scope", "$timeout", function ($scope, $timeout) { // $watch var watcher = $scope.$watch("value1", function (newVal, oldVal) { $scope.w1 = "$watch--" + "new:" + newVal + ";" + "old:" + oldVal; /* *注销$watch *太多的$watch将会导致性能问题,$watch如果不再使用,我们最好将其释放掉。 *$watch函数返回一个注销监听的函数,如果我们想监控一个属性,然后在稍后注销它,可以使用下面的方式: */ if (newVal == ‘clear‘) {//设置一个注销监听的条件 watcher(); //注销监听 } }); // $watchGroup $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) { //注意:newVal与oldVal都返回的是一个数组 $scope.w2 = "$watchGroup--" + "new:" + newVal + ";" + "old:" + oldVal; }); // $watchCollection $scope.arr = [‘nick‘, ‘ljy‘, ‘ljj‘, ‘zhw‘]; $scope.$watchCollection(‘arr‘, function (newVal, oldVal) { $scope.w3 = "$watchCollection--" + "new:" + newVal + ";" + "old:" + oldVal; }); $timeout(function () { $scope.arr = [‘my‘, ‘name‘, ‘is‘, ‘nick‘]; }, 2000); }]) // 小案例 .factory(‘watchService‘, [function () { var items = { goodsArr: [{ goods: ‘goods1‘, price: 10, num: ‘‘ }, { goods: ‘goods2‘, price: 20, num: ‘‘ }], sum: 0 }; return { getItemsSave: function () { return items; } }; }]) .controller(‘bodyCtl‘, [‘$scope‘, ‘watchService‘, function ($scope, watchService) { $scope.items = watchService.getItemsSave(); // 这里要监听数量变化计算综合 //一 只监听所有num变化计算总额 var watchArr = []; $scope.items.goodsArr.forEach(function (v, i) { watchArr.push("items.goodsArr[" + i + "][‘num‘]"); }); $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal与oldVal都返回的是一个数组 $scope.items.sum = 0; $scope.items.goodsArr.forEach(function (v, i) { $scope.items.sum += v.price * (v.num > 0 ? v.num : 0); }); }); /* //二 这样写则监听items.goodsArr所有成员 $scope.$watch(‘items.goodsArr‘, function () { $scope.items.sum = 0; $scope.items.goodsArr.forEach(function (v, i) { $scope.items.sum += v.price * (v.num > 0 ? v.num : 0); }); }, true);*/ }]) </script> </head> <body ng-controller="bodyCtl"> <div ng-view> <div ng-controller="ctrl"> <h2>$watch</h2> <div> <input type="text" ng-model="value1"/> </div> <div ng-bind="w1"></div> <h2>$watchGroup</h2> <div> <input type="text" ng-model="value2"/> <input type="text" ng-model="value3"/> </div> <div ng-bind="w2"></div> <h2>$watchCollection</h2> <ul> <li ng-repeat="v in arr" ng-bind="v"></li> </ul> <div ng-bind="w3"></div> </div> <h2>小案例</h2> <ul> <li ng-repeat="item in items.goodsArr"> <p ng-bind="item.goods"></p> <p> <span>单价:</span> <span ng-bind="item.price"></span> </p> <div> <input type="number" ng-model="item.num"> <span>个</span> </div> </li> </ul> <div> <span>总计:</span> <span ng-bind="items.sum"></span> <span>元</span> </div> </div> </body> </html>
angular之$watch、$watchGroup、$watchCollection
标签:cti enter dig called eval title each maximum size
原文地址:http://www.cnblogs.com/puyongsong/p/6803414.html