CaptainOfTheSky
CaptainOfTheSky

Reputation: 106

The line before a $timeout is not working (angularjs)

I have

vm.showSuccess = true;
$timeout(function() { close(); }, 2000);
vm.showSuccess = false;

The timeout function works fine, but the first line does not get fired.

I'm basically using this as a success message after a submit on a form

Upvotes: 0

Views: 32

Answers (1)

Ian
Ian

Reputation: 4185

You would have to change this to something like

vm.showSuccess = true;

$timeout(function () {
  close();
  vm.showSuccess = false;
}, 2000);

What's happening in your code is that the controller gets initialised and at that time vm.showSuccess is set to true, and on line 3 it gets set to false immediately, which is giving you the idea of it not getting executed.

Note how in my code example your last line is actually inside the $timeout so that is executes 2 seconds later.

Upvotes: 2

Related Questions