當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Angular JS 中的内置方法之$watch
生活随笔
收集整理的這篇文章主要介紹了
Angular JS 中的内置方法之$watch
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在$apply方法中存在臟檢查,首先apply方法會觸發evel方法,當evel方法解析成功后,會去觸發digest方法,digest方法會觸發watch方法。
$watch(watchFn,watchAction,deepWatch)
watchFn: angular表達式或函數的字符串
watchAction(newValue,oldValue,scope): watchFn發生變化會被調用deepWatch:可選的布爾值 檢查被監控的對象的每個屬性是否發生變化
$watch會返回一個函數,想要注銷這個watch可以使用函數
例如,實現一個當修改用戶名超過五次就報警提示的功能
需要注意的是,$watch 僅僅作為一個監視器的存在,不能阻止你繼續修改input內的值。
angular.module('MyApp',[]) .controller('ParserController',function($scope){$scope.count = 0;$scope.$watch('name',function(newVal,oldVal){++$scope.count ;if($scope.count > 5){alert("hahaha")}}); });?
<!DOCTYPE html> <html> <head><meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Page Title</title><script src="../../src/angular.js"></script><script src="parser.js"></script> </head> <body ng-app="MyApp"><div ng-controller="ParserController"><input ng-model="name"></div> </body> </html>如果監聽的是一個對象,而不是一個普通的數值或字符串,那么需要將第三個參數設置為true,這時候才會完全對比前后兩次修改的對象是否內容改變,而不是引用改變才觸發監聽事件。
轉載于:https://www.cnblogs.com/Pikzas/p/8982690.html
總結
以上是生活随笔為你收集整理的Angular JS 中的内置方法之$watch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell脚本if条件总结
- 下一篇: gradle idea java ssm