How to watch component binding change using Angular component How to watch component binding change using Angular component angularjs angularjs

How to watch component binding change using Angular component


You can add the $onChanges method to the controller

$onChanges(changesObj) is called whenever one-way bindings are updated. The changesObj is a hash whose keys are the names of the bound properties that have changed, and the values are an object of the form.

Following example handles canChange change event.

angular.module('app.components', []).component('changeHandler', {  controller: function ChangeHandlerController() {    this.$onChanges = function (changes) {      if (changes.canChange)        this.performActionWithValueOf(changes.canChange);    };  },  bindings: {    canChange: '<'  },  templateUrl: 'change-handler.html'});