国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS中watch監(jiān)聽用法分析

2019-11-19 19:04:12
字體:
供稿:網(wǎng)友

本文實(shí)例講述了AngularJS中watch監(jiān)聽用法。分享給大家供大家參考,具體如下:

ANGULAR 監(jiān)聽使用:

當(dāng)angular數(shù)據(jù)模型發(fā)生變化時(shí),我們需要如果需要根據(jù)他的變化觸發(fā)其他的事件。

$watch是一個(gè)scope函數(shù),用于監(jiān)聽模型變化,當(dāng)你的模型部分發(fā)生變化時(shí)它會(huì)通知你。

$watch(watchExpression, listener, objectEquality);

watchExpression 需要監(jiān)控的表達(dá)式
listener 處理函數(shù),函數(shù)參數(shù)如下 
function(newValue,oldValue, scope)
objectEquality 是否深度監(jiān)聽,如果設(shè)置為true,它告訴Angular檢查所監(jiān)控的對(duì)象中每一個(gè)屬性的變化. 如果你希望監(jiān)控?cái)?shù)組的個(gè)別元素或者對(duì)象的屬性而不是一個(gè)普通的值, 那么你應(yīng)該使用它

<!DOCTYPE html><html ng-app="app"><head><meta charset="UTF-8"><title>Insert title here</title></head><script src="assets/angular.min.js"></script><script src="assets/js/jquery.min.js"></script><script type="text/javascript">var app=angular.module("app",[]);app.controller('MainCtrl', function($scope) {  $scope.name = "Angular";  $scope.updated = -1;  $scope.$watch('name', function(newValue, oldValue) {   if (newValue === oldValue) { return; } // AKA first run   $scope.updated++;  });  var i=0;  $scope.getScope=function(){   // console.info(this);   var obj=$("#btnTest");   i++;   angular.element( obj).scope().name="hello" +i;  } });</script><body ng-controller="MainCtrl"> <input ng-model="name" /> Name updated: {{updated}} times. <button id="btnTest" ng-click="getScope()">獲取scope</button></body></html>

此代碼監(jiān)控$scope的name值的變化,如果發(fā)生變化則觸發(fā)監(jiān)聽。

監(jiān)控對(duì)象:

<!DOCTYPE html><html ng-app="app"><head><meta charset="UTF-8"><title>Insert title here</title></head><script src="assets/angular.min.js"></script><script src="assets/js/jquery.min.js"></script><script type="text/javascript">var app=angular.module("app",[]);app.controller('MainCtrl', function($scope) { $scope.user = { name: "Fox" };  $scope.updated = -1;  var watch=$scope.$watch('user', function(newValue, oldValue) {  if (newValue === oldValue) { return; }  $scope.updated++;  $scope.$broadcast('userUpdate', newValue.name);  });  //watch();  var i=0;  $scope.$on('userUpdate',function(d,data){   console.info(data);  })  $scope.getScope=function(){   // console.info(this);   var obj=$("#btnTest");   i++;   angular.element( obj).scope().user.name="hello" +i;  } });</script><body ng-controller="MainCtrl"> <input ng-model="user.name" /> Name updated: {{updated}} times. <button id="btnTest" ng-click="getScope()">獲取scope</button></body></html>

這里我們點(diǎn)擊按鈕會(huì)發(fā)現(xiàn)監(jiān)控并不會(huì)觸發(fā),原因是我們監(jiān)控了user對(duì)象,這個(gè)user對(duì)象沒喲發(fā)生變化,只不過屬性值發(fā)生了變化。

如果我們希望監(jiān)控user對(duì)象屬性發(fā)生變化,有兩個(gè)做法。

1.使用深度監(jiān)控。

方法如下:

<!DOCTYPE html><html ng-app="app"><head><meta charset="UTF-8"><title>Insert title here</title></head><script src="assets/angular.min.js"></script><script src="assets/js/jquery.min.js"></script><script type="text/javascript">var app=angular.module("app",[]);app.controller('MainCtrl', function($scope) { $scope.user = { name: "Fox" };  $scope.updated = -1;  var watch=$scope.$watch('user', function(newValue, oldValue) {  if (newValue === oldValue) { return; }  $scope.updated++;  $scope.$broadcast('userUpdate', newValue.name);  },true);  //watch();  var i=0;  $scope.$on('userUpdate',function(d,data){   console.info(data);  })  $scope.getScope=function(){   // console.info(this);   var obj=$("#btnTest");   i++;   angular.element( obj).scope().user.name="hello" +i;  } });</script><body ng-controller="MainCtrl"> <input ng-model="user.name" /> Name updated: {{updated}} times. <button id="btnTest" ng-click="getScope()">獲取scope</button></body></html>

設(shè)置為深度監(jiān)控,只要對(duì)象發(fā)生變化,那么監(jiān)聽就會(huì)觸發(fā)。

2.直接寫對(duì)象的屬性值路徑。

var watch=$scope.$watch('user.name', function(newValue, oldValue) {//具體代碼就不全部寫了。

消除監(jiān)聽

系統(tǒng)中太多的監(jiān)聽會(huì)影響系統(tǒng)的性能,我們可以在滿足某些條件后,去掉監(jiān)聽。

去掉監(jiān)聽方法如下:

var watch=$scope.$watch('user', function(newValue, oldValue) {  if (newValue === oldValue) { return; }  $scope.updated++;  $scope.$broadcast('userUpdate', newValue.name);  },true);//去掉監(jiān)聽。watch();

在系統(tǒng)中使用事件廣播。

比如在監(jiān)聽時(shí),我們對(duì)外廣播一個(gè)事件,

在控制其中寫監(jiān)聽的處理方法:

實(shí)例如下:

$scope.$broadcast('userUpdate', newValue.name);

監(jiān)聽代碼:

$scope.$on('userUpdate',function(d,data){ console.info(data);})

這種做法最好使用在指令中,指令中廣播事件,在控制器中實(shí)現(xiàn)監(jiān)聽。好處在于實(shí)現(xiàn)代碼的重用。

希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 姚安县| 中西区| 伊宁县| 瑞金市| 西平县| 赞皇县| 封开县| 冀州市| 海安县| 保德县| 浦县| 年辖:市辖区| 新龙县| 湖南省| 微山县| 隆化县| 昌邑市| 温泉县| 万宁市| 平乡县| 志丹县| 江阴市| 岑巩县| 慈溪市| 景泰县| 宁海县| 资溪县| 镇远县| 保定市| 新建县| 阿拉尔市| 武宁县| 双峰县| 莎车县| 都兰县| 桃园市| 茂名市| 固阳县| 云霄县| 琼中| 岑溪市|