Angular中的$watch、$watchGroup、$watchCollection
• 1,原型:$watch: function(watchExp, listener, objectEquality, prettyPrintExpression){};
• 2,參數(shù):watchExp(必須):{(function()|string)},可以字符串表達(dá)式,也可以帶當(dāng)前scope為參數(shù)的函數(shù)
• - `string`: Evaluated as {@link guide/expression expression}
• - `function(scope)`: called with current `scope` as a parameter.
• 3,參數(shù):listener(必須):function(newVal, oldVal, scope),觀察的表達(dá)式變化的時(shí)候調(diào)用的函數(shù)。
• 4,參數(shù):objectEquality(非必須):是否監(jiān)視個(gè)對(duì)象,默認(rèn)為false
• 5,$scope.$digest().會(huì)執(zhí)行所有的同$scope下的$watch。
• 但會(huì)出錯(cuò)$apply already in progress,換了$rootScope也一樣。
• 原因-參考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643
• $digest、$apply、$$phase這些屬性或者方法其實(shí)都是$scope中的私有的,最好不要使用。
• 6,$watch一個(gè)對(duì)象。
• 如果要監(jiān)視對(duì)象的變化(地址改變),$watch對(duì)象名,第三個(gè)參數(shù)默認(rèn);
• 如果監(jiān)測(cè)對(duì)象中某一屬性,可以寫user.name的形式,第三個(gè)參數(shù)默認(rèn);
• 如果監(jiān)測(cè)對(duì)象中全部屬性,$watch對(duì)象名,第三個(gè)參數(shù)true;
• 7,$watchGroup,第一個(gè)參數(shù)是一個(gè)表達(dá)式的數(shù)組或者返回表達(dá)式的數(shù)組的函數(shù)。
• 8,$watchCollection;
• js中數(shù)組也是對(duì)象,但按照$watch一個(gè)對(duì)象的方式,只有數(shù)組引用變了才能監(jiān)聽(tīng)變化,增加刪除$watch監(jiān)聽(tīng)不到,所以就有了$watchCollection。
• function(obj, listener):第一個(gè)參數(shù)必須對(duì)象或者返回對(duì)象的函數(shù)。
•9,注銷$watch
• $watch函數(shù)返回一個(gè)注銷監(jiān)聽(tīng)的函數(shù),太多的$watch將會(huì)導(dǎo)致性能問(wèn)題,$watch如果不再使用,我們最好將其釋放掉。
一、使用方法
html
<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>
js
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') {//設(shè)置一個(gè)注銷監(jiān)聽(tīng)的條件 watcher(); //注銷監(jiān)聽(tīng) } }); // $watchGroup $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個(gè)數(shù)組 $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); }])
二、小案例
html
<h2>小案例</h2> <ul> <li ng-repeat="item in items.goodsArr"> <p ng-bind="item.goods"></p> <p> <span>單價(jià):</span> <span ng-bind="item.price"></span> </p> <div> <input type="number" ng-model="item.num"> <span>個(gè)</span> </div> </li> </ul> <div> <span>總計(jì):</span> <span ng-bind="items.sum"></span> <span>元</span> </div>
js
// 小案例 .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(); // 這里要監(jiān)聽(tīng)數(shù)量變化計(jì)算綜合 //一 只監(jiān)聽(tīng)所有num變化計(jì)算總額 var watchArr = []; $scope.items.goodsArr.forEach(function (v, i) { watchArr.push("items.goodsArr[" + i + "]['num']"); }); $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個(gè)數(shù)組 $scope.items.sum = 0; $scope.items.goodsArr.forEach(function (v, i) { $scope.items.sum += v.price * (v.num > 0 ? v.num : 0); }); }); /* //二 這樣寫則監(jiān)聽(tīng)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,參數(shù):watchExp(必須):{(function()|string)},可以字符串表達(dá)式,也可以帶當(dāng)前scope為參數(shù)的函數(shù) * - `string`: Evaluated as {@link guide/expression expression} * - `function(scope)`: called with current `scope` as a parameter. * 3,參數(shù):listener(必須):function(newVal, oldVal, scope),觀察的表達(dá)式變化的時(shí)候調(diào)用的函數(shù)。 * 4,參數(shù):objectEquality(非必須):是否監(jiān)視個(gè)對(duì)象,默認(rèn)為false * 5,$scope.$digest().會(huì)執(zhí)行所有的同$scope下的$watch。 * 但會(huì)出錯(cuò)$apply already in progress,換了$rootScope也一樣。 * 原因-參考大牛博客:http://blog.csdn.net/aitangyong/article/details/48972643 * $digest、$apply、$$phase這些屬性或者方法其實(shí)都是$scope中的私有的,最好不要使用。 * 6,$watch一個(gè)對(duì)象。 * 如果要監(jiān)視對(duì)象的變化(地址改變),$watch對(duì)象名,第三個(gè)參數(shù)默認(rèn); * 如果監(jiān)測(cè)對(duì)象中某一屬性,可以寫user.name的形式,第三個(gè)參數(shù)默認(rèn); * 如果監(jiān)測(cè)對(duì)象中全部屬性,$watch對(duì)象名,第三個(gè)參數(shù)true; * 7,$watchGroup,第一個(gè)參數(shù)是一個(gè)表達(dá)式的數(shù)組或者返回表達(dá)式的數(shù)組的函數(shù)。 * 8,$watchCollection; * js中數(shù)組也是對(duì)象,但按照$watch一個(gè)對(duì)象的方式,只有數(shù)組引用變了才能監(jiān)聽(tīng)變化,增加刪除$watch監(jiān)聽(tīng)不到,所以就有了$watchCollection。 * function(obj, listener):第一個(gè)參數(shù)必須對(duì)象或者返回對(duì)象的函數(shù)。 */ 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將會(huì)導(dǎo)致性能問(wèn)題,$watch如果不再使用,我們最好將其釋放掉。 *$watch函數(shù)返回一個(gè)注銷監(jiān)聽(tīng)的函數(shù),如果我們想監(jiān)控一個(gè)屬性,然后在稍后注銷它,可以使用下面的方式: */ if (newVal == 'clear') {//設(shè)置一個(gè)注銷監(jiān)聽(tīng)的條件 watcher(); //注銷監(jiān)聽(tīng) } }); // $watchGroup $scope.$watchGroup(["value2", "value3"], function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個(gè)數(shù)組 $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(); // 這里要監(jiān)聽(tīng)數(shù)量變化計(jì)算綜合 //一 只監(jiān)聽(tīng)所有num變化計(jì)算總額 var watchArr = []; $scope.items.goodsArr.forEach(function (v, i) { watchArr.push("items.goodsArr[" + i + "]['num']"); }); $scope.$watchGroup(watchArr, function (newVal, oldVal) { //注意:newVal與oldVal都返回的是一個(gè)數(shù)組 $scope.items.sum = 0; $scope.items.goodsArr.forEach(function (v, i) { $scope.items.sum += v.price * (v.num > 0 ? v.num : 0); }); }); /* //二 這樣寫則監(jiān)聽(tīng)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>單價(jià):</span> <span ng-bind="item.price"></span> </p> <div> <input type="number" ng-model="item.num"> <span>個(gè)</span> </div> </li> </ul> <div> <span>總計(jì):</span> <span ng-bind="items.sum"></span> <span>元</span> </div> </div> </body> </html>
以上所述是小編給大家介紹的Angular中的$watch、$watchGroup、$watchCollection,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Angular2 自定義表單驗(yàn)證器的實(shí)現(xiàn)方法
這篇文章主要介紹了Angular2 自定義表單驗(yàn)證器的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12如何在Angular8.0下使用ngx-translate進(jìn)行國(guó)際化配置
這篇文章主要介紹了如何在Angular8.0下使用ngx-translate進(jìn)行國(guó)際化配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07詳解如何構(gòu)建一個(gè)Angular6的第三方npm包
這篇文章主要介紹了詳解如何構(gòu)建一個(gè)Angular6的第三方npm包,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Angular+ionic實(shí)現(xiàn)折疊展開(kāi)效果的示例代碼
這篇文章主要介紹了Angular+ionic實(shí)現(xiàn)折疊展開(kāi)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07使用ionic播放輪詢廣告的實(shí)現(xiàn)方法(必看)
下面小編就為大家?guī)?lái)一篇使用ionic播放輪詢廣告的實(shí)現(xiàn)方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04AngularJS自定義過(guò)濾器用法經(jīng)典實(shí)例總結(jié)
這篇文章主要介紹了AngularJS自定義過(guò)濾器用法,結(jié)合實(shí)例形式總結(jié)分析了AngularJS自定義過(guò)濾器進(jìn)行包含、替換、篩選、過(guò)濾、排序等操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05使用AngularJS對(duì)表單提交內(nèi)容進(jìn)行驗(yàn)證的操作方法
AngularJS是一款優(yōu)秀的前端JS框架,已經(jīng)被用于Google的多款產(chǎn)品當(dāng)中。下面通過(guò)本文給大家分享使用AngularJS對(duì)表單提交內(nèi)容進(jìn)行驗(yàn)證的操作方法,需要的的朋友參考下吧2017-07-07