Miron Miron - 1 year ago 69
AngularJS Question

Angularjs: 'controller as syntax' and $watch

How to subscribe on property change when using "controller as syntax"?

controller('TestCtrl', function ($scope) {
this.name = 'Max';
this.changeName = function () {
this.name = new Date();
// not working

<div ng-controller="TestCtrl as test">
<input type="text" ng-model="test.name" />
<a ng-click="test.changeName()" href="#">Change Name</a>

Answer Source

Just bind the relevant context.

$scope.$watch(angular.bind(this, function () {
  return this.name;
}), function (newVal) {
  console.log('Name changed to ' + newVal);

Example: http://jsbin.com/yinadoce/1/edit


Bogdan Gersak's answer is actually kind of equivalent, both answers try binding this with the right context. However, I found his answer cleaner.

Having that said, first and foremost, you have to understand the underlying idea behind it.


For those who use ES6, by using arrow function you get a function with the right context OOTB.

$scope.$watch(() => this.name, function (newVal) {
  console.log('Name changed to ' + newVal);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download