1

I have a controller in which a value gets randomly generated

app.controller('detailReadingCtrl',function(){
  var value = 0;
  $scope.dispValue = 0;

 setInterval(function(){
   value = Math.floor(Math.random()*1000);
   $scope.dispValue = value;
 }
});

and my html is

<div>{{dispValue}}</div>

The Value on the html is not getting updated with the changed value. The dispvalue is changing in the controller but it is not updating in the html. I want to see the value change for every second on the screen without the need of refresh.

I tried using $scope.$watch and also $scope.$broadcast both seem to not work. Please let me know if you have any ideas about this.

1 Answer 1

3

setInterval doesn't trigger a $digest cycle, instead, use the $interval module:

app.controller('detailReadingCtrl',function($scope, $interval){
    var value = 0;
    $scope.dispValue = 0;

    $interval(function(){
        value = Math.floor(Math.random()*1000);
        $scope.dispValue = value;
    });
});

And you had other errors in the code ($scope wasn't being injected, missing ) at the end of the interval)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I am able to see the change in the values. Thanks for the help. I removed the other errors too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.