How to use $watch to monitor changes to a variable and log them to the console in angularJS

<div ng-app="watchApp" ng-controller="WatchController">
    <input type="text" ng-model="watchedValue" placeholder="Type something">
</div>

<script>
    var app = angular.module('watchApp', []);
    app.controller('WatchController', function($scope) {
        $scope.watchedValue = '';

        $scope.$watch('watchedValue', function(newVal, oldVal) {
            console.log('Changed from', oldVal, 'to', newVal);
        });
    });
</script>

 

Post your Answer