To use #watchGroup using a controller as syntax, you will first need to declare your controller as follows:
app.controller('Controller1', ['$scope', Controller1]); function Controller1($scope) { var vm = this; vm.firstName = 'Agustin'; vm.lastName = 'Cassani'; vm.newVal = ''; vm.oldVal = ''; $scope.$watchGroup(['vm.firstName', 'vm.lastName'], function (newValue, oldValue) { vm.newVal = newValue; vm.oldVal = oldValue; }); }
As you can see, we made a reference to "this" in the vm variable.
In the routing configuration of your application, you will have something like this if, of course, you declare your controllers to determine the routes:
var app = angular.module('app', ['ngRoute']); app.config(['$routeProvider', function ($routeProvider) { $routeProvider. when('/', { templateUrl: 'app/views/view1.html', controller: 'Controller1', controllerAs: 'vm' }). otherwise({ redirectTo: '/' }); }]);
Finally, the html of our view would be something like this:
<input type="text" data-ng-model="vm.firstName" /> <input type="text" data-ng-model="vm.lastName"/> <br/> <div> {{vm.newVal}} </div> <div> {{vm.oldVal}} </div>
IHIH (hope this helps!)
source share