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?
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).
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.
$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.
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.
function MyCtrl($scope, $timeout) {
$scope.rand = 0;
(function update() {
$timeout(update, 1000);
$scope.rand = Math.random() * 10;
}());
}
demo: http://jsbin.com/udagop/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With