Angularjs Form Validation

Source: Internet
Author: User

with AngularJS instructions to handle focus and blur events (Translate blog post, original post address: http://blog.ejci.net/2013/08/06/ dealing-with-focus-and-blur-in-angularjs-directives/)

with AngularJS instructions to handle focus and blur events

I'm developing an appthat uses AngularJS. I have a problem that is how to set the focus on a form element. There is no ng-focus or Ng-blur in the AngularJS Directive. So here's a way for me to solve this problem, I didn't say it was a very good way, but I helped me solve the problem.

?

Problem description

I have a Web Server, sometimes you need to get some extra information from the user. To improve the user experience, I want to add focus to the form to get the attention of the consumer. Basically, I send a post request to the server and get a response from the server, and the response requires the user to fill in some additional parameters (security certificate, like:Captcha,GRID ...). the task is to create a nice overlayon the page, and then give the form element the focus after getting the response returned from the server.

?

Solve:

HTML Code

JavaScript Code

it's simple. Listen to the $scope by customizing the command my-focus. focusfield1/2 two variables. If the value of these two variables is true, the focusevent is triggered, while the focus listener sets the focus on the element.

In the event of a loss of focus, I define another directive , My-blur, which invokes the method defined for the lost Focus event, which is called when the focus event is lost, and the focus variable is reset back to False.

To summarize, I have to change the values in the scope to get the focus on an element in the page, which I don't think is good, but I haven't found a better way. If you have a better solution, please let me know.

Angularjs Form Validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.