참고(구글번역)
– Understanding $watch(), $watchgroup() and $watchCollection() methods of scope
(http://www.dotnettricks.com/learn/angularjs/understanding-watch-watchgroup-and-watchcollection-methods-of-scope)
■ Angular는 $scope에서 모델 변경을 관찰하기 위해 $ watch API를 사용합니다. $scope에있는 각 변수의 관찰자는 값의 변화를 관찰합니다. $scope의 변수 값이 변경되면 뷰는 자동으로 업데이트됩니다. $ watch API는 모델 변경 사항을 스코프에서 관찰하는 방법으로 $watch, $watchGroup, $watchCollection가 있다.
■ $watch
이 함수는 $ scope에있는 변수의 변경을 관찰하는 데 사용됩니다. watchExpression, listener및 objectEquality의 세 가지 매개 변수를 사용합니다. 여기서 listener 및 objectEquality는 선택 매개 변수입니다.(listener는 변경전/후 값을 매개변수로 가지는 함수)
$watch(watchExpression, listener, [objectEquality])
여기에서 watchExpression은 $scope 범위의 표현식입니다. 이 표현식은 $ digest ()를 호출 할 때마다 호출되며 감시되는 값을 반환합니다. 리스너는 watchExpression의 값이 새 값으로 변경 될 때 호출되는 함수를 정의합니다. watchExpression이 변경되지 않으면 리스너가 호출되지 않습니다.
objectEquality는 참조 equality를 비교하는 대신 angular.equals를 사용하여 객체가 동일한 지 비교하는 데 사용되는 boolean type입니다.
<script> scope.name = 'shailendra'; scope.counter = 0; scope.$watch('name', function (newVal, oldVal) { scope.counter = scope.counter + 1; }); </script>
■ $watch 예제
<!DOCTYPE html> <html ng-app="myApp"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8"> <head> <title></title> <script src="/js/angularjs/1.5.6/angular.js"></script> <script src='/js/jquery/jquery-1.11.0.min.js'></script> <script> var myApp = angular.module("myApp",[]); myApp.controller("myAppCtrl", function($scope){ $scope.count = 0; $scope.countWatch = function() { $scope.count = $scope.count + 1; } $scope.$watch('count', function(newVal, oldVal) { console.log( "newVal : " + newVal + "\toldVal : " + oldVal ); $("#log").text( $scope.count ); }); }); </script> </head> <body ng-controller="myAppCtrl"> <h1> AngularJS $watch<br/> <font color="red"></font> </h1> <button ng-click="countWatch()">$watch 테스트(count+1)</button> <hr/> Count : <div id="log"></div> </body> </html>
■ $watch 예제 실행 결과
- AngularJS angular.copy 사용법, angular 복사 2019년 5월 17일
- AngularJS fromJson, toJson 사용법 2019년 5월 15일
- AngularJS에서 ng-style사용시 조건문 이용방법 2019년 4월 25일
- AngularJS에서 ng-style사용시 함수 이용방법 2019년 4월 23일
- AngularJS 동적 HTML, Function 추가 2019년 4월 15일
- AngularJS에서 팝업창 띄우기 2019년 4월 2일
- AngularJS 하나의 모듈에 factory 다중 등록 2018년 2월 26일
- AngularJS factory 예제 2018년 2월 26일
- AngularJS forEach ng-repeat 예제 2018년 2월 19일
- AngularJS $http, spring TEXT response 예제 2018년 1월 17일
- AngularJS selectbox 간단한 예제 2018년 1월 15일
- AnuglarJS forEach 예제 2018년 1월 12일
- AngularJS checkbox 제어 및 활성/비활성(ng-disabled) 예제 2018년 1월 8일
- AngularJS button 활성/비활성(ng-disabled) 예제 2018년 1월 8일
- AngularJS radio 활성/비활성(ng-disabled) 예제 2018년 1월 8일
- AngularJS $broadcast, $emit, $on 예제 2018년 1월 3일
- AngularJS – $watch $digest $apply Life Cycle 2017년 12월 26일
- AngularJS $watchCollection 2017년 12월 26일
- AngularJS $watchGroup 2017년 12월 26일
- AngularJS $watch 2017년 12월 26일
- AngularJS 반복문 ng-repeat 2017년 7월 22일
- AngularJS $timeout 2017년 7월 22일
- AngularJS Show/Hide, 보이기/숨기기 2017년 7월 16일
- AngularJS 라디오 버튼 제어 2017년 7월 16일
- AngularJS시작하기, Hello AngularJS 2017년 7월 15일