您好,登錄后才能下訂單哦!
$Watch:(監(jiān)聽一個model,當(dāng)一個model每次改變時,都會觸發(fā)第二個函數(shù))
$watch('watchFn',watchAction,deepWatch)
watchFn:帶有Angular 表達(dá)式或者函數(shù)的字符串,它會返回被監(jiān)控的數(shù)據(jù)模型的當(dāng)前值。
watchAction: 一個函數(shù)function(newValue,oldValue){},當(dāng)watchFn 發(fā)生變化時會被調(diào)用
deepWatch:默認(rèn)為false,監(jiān)聽數(shù)組的某個元素或者對象的屬性時設(shè)置為true;
監(jiān)控一個屬性:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <form> <div class="form-group"> <input ng-model="name" />{{name}} <p>改變次數(shù){{count}}</p> </div> </form> </div> <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope){ $scope.name = '橘子'; $scope.count = 0; $scope.$watch('name', function (newValue, oldValue) { $scope.count++; if($scope.count >5){ $scope.name = '蘋果'; } }); }); </script> </body> </html>
監(jiān)控一個對象(deepWidth為true)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> <form> <div class="form-group"> <input ng-model="Data.name" />{{Data.name}} <p>改變次數(shù){{count}}</p> </div> </form> </div> <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope){ $scope.Data = { name: '橘子' }; $scope.count = 0; $scope.$watch('Data', function (newValue, oldValue) { if(newValue == oldValue) return; $scope.count++; if($scope.count >5){ $scope.Data.name = '蘋果'; } }, true); }); </script> </body> </html>
以上這篇關(guān)于angular js_$watch監(jiān)控屬性和對象詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。