Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS. Autorefresh $scope variable in html

How can I trig automatic my variable at $scope object?

//controller
setInterval(function(){$scope.rand=Math.random(10)},1000);

//template
{{rand}}

Rand is not update on my page. How can I update my variable?

like image 316
Paul Kononenko Avatar asked Jan 24 '13 11:01

Paul Kononenko


People also ask

What is the use of $scope variable in AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

What is the difference between $scope and scope in AngularJS?

The $ in "$scope" indicates that the scope value is being injected into the current context. $scope is a service provided by $scopeProvider . You can inject it into controllers, directives or other services using Angular's built-in dependency injector: module.

What is the use of $Watch in AngularJS?

$watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in Angularjs to handle variable changes in application.

What is the use of $scope and $rootScope angular object?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.


1 Answers

function MyCtrl($scope, $timeout) {
  $scope.rand = 0;

  (function update() {
    $timeout(update, 1000);
    $scope.rand = Math.random() * 10;
  }());
}

demo: http://jsbin.com/udagop/1/

like image 104
Yoshi Avatar answered Oct 08 '22 20:10

Yoshi