A watch function is passed two attributes, the new value and the old value. 
I would compare both to see if it has changed before performing execution. 
As you are generating new date objects, a watch will always fire. So:

$scope.$watch('endDate', function(newDate, oldDate) {

  if (newDate.getTime() === oldDate.getTime()) {
    return;
  }

  ...

});

You could also consider combining your watch functions into one, e.g:

$scope.$watch(function () {
  return [$scope.startDate, $scope.endDate, $scope.durationMillis];
}, function () {
  ...
}, true);

On Thursday, 20 November 2014 22:15:56 UTC+11, Ramaraj Karuppusamy wrote:
>
> Hi All,
>
> I have three models startDate,endDate,durationMillis in scope. If any of 
> the value is changed by use, I need to calculate the other model by $watch.
>
> $scope.$watch('startDate', function(){
>     $scope.endDate = new Date($scope.startDate.getTime() + 
> $scope.durationMillis);});
>
> $scope.$watch('endDate', function(){
>     $scope.durationMillis = $scope.endDate.getTime() + 
> $scope.startDate.getTime();});
>
> $scope.$watch('durationMillis', function(){
>     $scope.startDate = new Date($scope.endDate.getTime() - 
> $scope.durationMillis.getTime());});
>
> Here, my problem is when user changes start date/end date/duration, $watch 
> of calculated attributes(not changed by user) has been called recursively.
>
> Please give a solution to stop other watches when calculating the value.
>
>
> Thanks and Regards
>
> Ramaraj K
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to