I have the code:
if (xxx == xxx){
var x = 5 + 3;
setTimeout(function() {
$('.regErrMsg').text("");
$scope.errMsg = "Hi.";
}, 5000);
}
I would like to execute the function i.e, show "Hi" message after 5 seconds. So, is my code correct. As of now, the message is not showing up. Where have I gone wrong?
The "problem" with setTimeout inside your controller is that angular is not going to watch the content of the scope after the function in the setTimeout is called.
You can do $scope.$apply() to force the view to update, eg:
setTimeout(function() {
$scope.errMsg = "Hi.";
$scope.$apply();
}, 5000);
They is a special function $timeout which will trigger the view refresh automatically after the function has been called. It has the same signature as setTimeout.
$timeout(function() {
$scope.errMsg = "Hi.";
//You don't need $scope.$apply() here
}, 5000);
You should inject $timeout into your controller
I have removed the $('.regErrMsg').text(""); because you shouldn't change the dom inside a controller.
My question is .. does the Hi executes and then it waits for 5 sec or wait waits for 5 secs and then shows Hi ?
Let's consider a simplified example
foo();
setTimeout(function () {bar();}, 5000);
baz();
Now it's easier to describe what will happen, step by step (in excruciating detail)
foo gets interpreted() invokes foosetTimeout gets interpretedsetTimeout are interpreted, i.e. function references set here(/* ... */) invokes setTimeoutsetTimeout sets up a callback to invoke argument 0 after argument 1 millisecondsbaz gets interpreted() invokes baz0 (from 5) gets invokedbar gets interpreted (using references from 4)() invokes barAs of now, the message is not showing up. Where have I gone wrong?
It looks like the only change you've made that will be reflected in the DOM is the clearing of text from .regErrMsg, perhaps you meant to use
$('.regErrMsg').text("Hi.");
or invoke some other method which will make the updated vale of $scope be reflected in the #document