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