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

