Hi,
I need to update my input using some interval. So I'm using following code:
HTML
<div ng-app="myapp">
<div ng-controller="MyCtrl">
<input type="text" ng-model="testVal" />
</div>
</div>
and JS:
var app = angular.module('test', []);
app.controller('MyCtrl', function ($scope) {
$scope.testVal = 0;
setInterval(function() {
$scope.testVal++;
}, 1000);
});
When I'm debugging code then testVal is updated, but <input> have initial value.
Why?
You should use $timeout function from Angular.
Example:
var myapp = angular.module("myapp", []);
myapp.controller("MyController", function($scope, $timeout){
$scope.testVal = 0;
$timeout(function() {
// Some code
$scope.testVal++;
$scope.apply(); // This function is updating your view, but it does not necessarily
}, 3000);
});
FavScripts.com is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.
Boost your productivity with FavScripts.com!