-1

I am trying to find out a way to write a Angular JS directive which will set focus on a textbox after saving data using $http.

I am trying to solve the following problem.

  1. User fill up a form
  2. On ng-submit I save the data using $http call.
  3. I want to put the focus to first element of form after successfull save.

As I understand that DOM manipulation needs to be done via Directive. Can anyone provide a simple directive sample code to do this?

  • UPDATE - I should put my code with my question. Here it is

Sorry I should put my code before. Here is what I tried but didn't work

Html Code

learner
  • 129
  • 9
  • possible duplicate of: http://stackoverflow.com/questions/14833326/how-to-set-focus-in-angularjs – Wottensprels Jul 12 '13 at 13:29
  • I didn't put any code for $http call but put some comments to make my intention clear, what I am trying to achieve. – learner Jul 13 '13 at 12:12

1 Answers1

1

Your question does not show any research effort and is therefore likely to stay unanswered or get closed (besides the fact that it is a duplicate). Nevertheless, here some simple example code

app.directive('focusMe', function($timeout) {
 return {
   link: function(scope, element) {

        element[0].focus(); 

    }
  });
}

Within a directives linking function, you have access to the element. Use this to handle it. See also:

Angular API: element

Wottensprels
  • 3,307
  • 2
  • 29
  • 38
  • I added my research effort. Can you put any light how to make this work? – learner Jul 13 '13 at 12:10
  • 1
    element[0] --- I was simply trying on element and was getting a undefined function exception... Not realizing element is an array . Which i cannot comprehend why..!! The directive is an attribute.. can be applied to one element only.. !! – Roshan Khandelwal Jan 31 '15 at 15:22