Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Directive with controllerAs, bindToController, and $scope.$watch

I have a fairly straightforward AngularJS question to which I cannot seem to find an answer:

How would I go about using $scope.$watch() in a directive controller while also using the controllerAs and bindToController options?

Please let me know if you need any clarification on what I mean exactly.

like image 406
Dean Zaslow Avatar asked Dec 19 '22 19:12

Dean Zaslow


1 Answers

Well, $scope.$watch watches for expressions so assuming you're binding your controller to the name vm (e.g. controllerAs: 'vm') you should use

$scope.$watch('vm.somethingToWatch', function(newval, oldval) {...})

You will need to still inject the $scope though, since $watch is not available on controller instances by themselves.

like image 65
ArtoAle Avatar answered Apr 19 '23 07:04

ArtoAle