1

I assume the form validation in AngularJS is delayed somehow, which is annoying. I have the following code in my directive's link function:

console.log(scope.signinForm.$invalid);   // TRUE
scope.signin.email = '[email protected]';
scope.signin.password = 'test';
console.log(scope.signinForm.$invalid);   // still TRUE, should be FALSE at this point

So I went ahead and used a setTimeout function:

var myFunction = function () {
  console.log(scope.signinForm.$invalid); // FALSE, which is correct
};

console.log(scope.signinForm.$invalid);   // TRUE
scope.signin.email = '[email protected]';
scope.signin.password = 'test';
setTimeout(myFunction, 500);

Can someone tell me what I need to do to update $invalid instantly?

Thank you,

Steven

3
  • Just wondering - why do you need to know the validity of the form inside the directive's JS? Commented Jun 25, 2014 at 16:51
  • @Ian - because if the form is invalid, I would like to avoid a server roundtrip when she hits the submit button. Commented Jun 26, 2014 at 7:02
  • Well, form validity is usually handled in the view, by preventing the submit button from being clicked there - whether you disable or hide it until the form is valid. I don't think the controller/directive should handle that kind of logic. If it matters, here's an example: jsfiddle.net/m5rg8 Commented Jun 26, 2014 at 13:41

1 Answer 1

1

You can try to apply the scope :

console.log(scope.signinForm.$invalid);   // TRUE
scope.signin.email = '[email protected]';
scope.signin.password = 'test';
scope.$apply();
console.log(scope.signinForm.$invalid);
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried that.. but then I get the following error: [$rootScope:inprog] $apply already in progress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.