0

currently I am using watch in angular Js ,but now we have upgraded to angular 1.5x and I don't want to use watch instead I am looking for some thing thing alternative to $scope.watch

I am using a global value defined in the App and I need some alternative to watch which is like a method to be called up when the value of the value is changed any where in the application.

dilbar505
  • 11
  • 1
  • 1
    you need to provide some code; however, even if you do, I'm not sure you'll like the responses, since you are describing the **exact purpose** of `$scope.$watch`, and not providing any reason why you don't want to use it. I suspect that in order to eliminate `$scope.$watch`, you'll have to re-design the entire architecture of your app. – Claies Apr 12 '17 at 11:01
  • 1
    I don't want to use $scope as its angular 1.5 x and want to get rid of $scope completely – dilbar505 Apr 12 '17 at 11:03
  • that sounds like a complete architecture change, which couldn't possibly be attempted with the information you provided here so far. As a side note, the whole idea that "`$scope` is bad, I don't want to use it" is ridiculous. Using scope **improperly** is a *common* issue, but it doesn't make `$scope` toxic. – Claies Apr 12 '17 at 11:05
  • @dilbar505 is on the way to release AngularJS 5. Be sensible. Its like you want to find an alternative for air and water. – Ankit Agarwal Apr 12 '17 at 11:06
  • Tend to agree with @Claies as you describe the purpose of $scope.$watch, why try to change that? Give an explanation as to why rather than 'I don't want to use watch', otherwise this is a question not worth answering. – rrd Apr 12 '17 at 11:13
  • Also, don't pick and choose from best practices. Don't just decide "using `$scope` is bad, but having a global value is ok...." – Claies Apr 12 '17 at 11:16
  • Possible duplicate of [AngularJs 1.5 - Component does not support Watchers, what is the work around?](https://stackoverflow.com/questions/35534479/angularjs-1-5-component-does-not-support-watchers-what-is-the-work-around) – Estus Flask Aug 02 '17 at 11:20

1 Answers1

0

There are basically 2 way of doeing without $watch...

1st) explicitly call the function For exemple if your variable is binded to a n input use ng-change to call the function you need.

2nd) use property setters. For exemple if you want to bind property p of your scope you can use this code in your controller:

var _p = 38;
Object.defineProperty(
    $scope, 
    "p", 
    {
        get : function(){ return _p; },
        set : function(val){ 
            _p = val;
            callSomeFunction(); // Do what you have been doing with $watch
        },
     );

But as already said in comments $scope.$watch is not the "evil thing" to avoid

Dmitry
  • 2,033
  • 1
  • 22
  • 31
  • Not evil. Just deprecated. As it's explained in [dupe question](https://stackoverflow.com/questions/35534479/angularjs-1-5-component-does-not-support-watchers-what-is-the-work-around). $scope.$apply does not have alternatives and still should be used. $scope.$watch has them. – Estus Flask Aug 02 '17 at 11:23