There is such code:

$scope.$watch(NgTimeManager.getTime(), function (time) { $scope.currentTime = time; }); 

Where:
NgTimeManager.getTime () - returns the current time (just a number, for example: 1501589214000).
$ scope.currentTime - I want to have the current time in this variable.

Maybe I misunderstand the principle of $ watch, for reasons I do not understand, currentTime variable is not updated. What could be the problem?

UPD rewrote as the documentation does not work:

  $scope.$watch(NgTimeManager.getTime(), function (newValue, oldValue) { if (newValue !== oldValue){ console.log("Changed"); $scope.currentTime = newValue; } }); 
  • This is not a watch. It does not work, this is nonsense written. Look at what parameters $watch accepts and see what you pass on to it - Grundy
  • In general, adding a watch to a constantly changing value is a mistake, you just immediately fall into the infinit digest - Grundy
  • rewrote as in the documentation, does not work - nope, it is NOT in the documentation - Grundy
  • what solution is better to apply in my case? - La_Concorde
  • 2
    Then use the advice to use $timeout or $interval services, they work out at specified intervals - Grundy

2 answers 2

Try this:

 $scope.$watch(function() { return NgTimeManager.getTime();}, function (newValue, oldValue) { if (newValue !== oldValue){ console.log("Changed"); $scope.currentTime = newValue; } }); 
  • not very good advice if NgTimeManager.getTime() will give different values ​​on each call - Grundy
  • @Grundy isn’t that what watch $ makes? the first function is called the first time, the value is saved, the first function is called the second time, compared with the previous one, if the values ​​are different, then we call the callback in which it is not necessary to check if (newValue! == oldValue). - Alexander
  • then we call the callback and after that we start the digest again, to make sure that nothing else has changed - Grundy
  • @Grundy, this is a question of application architecture. The original question was why $ watch does not work - Alexander
  • Not an application, namely an anguly - Grundy

Just run $ interval and do what you need in its callback.

 var app = angular.module("app",[]); app.controller("test", ['$interval','$scope',function($interval, $scope){ $interval(function(){$scope.time = new Date();}, 1000); }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <html> <body data-ng-app="app"> <div data-ng-controller="test"> <span data-ng-bind="time"></span> </div> </body> </html> 

But in general, this is a bad idea. The fact is that $ interval each time starts a digest which, in a lot of other work. And run it again is not worth it. I, in your place, would implement this slider on native js and wrapped it in a directive. So you get access to the slider, and the code will be cleaner, and you can, if anything, and play with the performance. If you do not need to update anything on the view (which is related to the slider), then $ intervala can be discarded in favor of the simple setInterval.