From 895ecffc875f822b2134d1f060d0e10219ac7faf Mon Sep 17 00:00:00 2001 From: sumit2chauhan Date: Sun, 6 Nov 2016 15:13:02 +0530 Subject: [PATCH] Updated README.md for $watch usage The property is not on the scope. Its available inside the controller only, it should be accessed by the watcher function expression. --- a1/README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/a1/README.md b/a1/README.md index edbbef19..678d425b 100644 --- a/a1/README.md +++ b/a1/README.md @@ -403,7 +403,7 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see var vm = this; ``` - Note: When creating watches in a controller using `controller as`, you can watch the `vm.*` member using the following syntax. (Create watches with caution as they add more load to the digest cycle.) + Note: When creating watches in a controller using `controller as`, you can watch the `vm.*` member using the following syntax (Create watches with caution as they add more load to the digest cycle). Also use a function as the first argument of the watcher, so that you remain in the scope of the controller. In this case vm.title would still be accessible when watcher is hit during digest cycle. ```html @@ -414,9 +414,12 @@ While this guide explains the *what*, *why* and *how*, I find it helpful to see var vm = this; vm.title = 'Some Title'; - $scope.$watch('vm.title', function(current, original) { - $log.info('vm.title was %s', original); - $log.info('vm.title is now %s', current); + $scope.$watch(function() { + return vm.title; + }, + function(current, original) { + $log.info('vm.title was %s', original); + $log.info('vm.title is now %s', current); }); } ```