2

I don't know how to focus on an input when clicking on a button in angularjs.

I create a simple demo which can't work here: http://plnkr.co/edit/NS0jJE9ttakNm8nc6QkZ?p=preview

This is the main code:

app.controller('MainCtrl', function($scope, $element) {
  $scope.name = 'World';
  $scope.myfocus = function(){
    $element.find("#myinput").???? // what to do here?
  }
});
3
  • 3
    Please, do not try to access to DOM from your controller. Use directives for that. Commented Mar 21, 2013 at 13:55
  • What will the directive be? Commented Mar 21, 2013 at 14:04
  • 2
    I just answered essentially the same question yesterday: stackoverflow.com/questions/15518772/… Commented Mar 21, 2013 at 14:48

1 Answer 1

2

You need to load jquery before loading angular.

See this updated plunker with load order changed and some changes to how you were trying to focus the element http://plnkr.co/edit/PLi1BiI83GKdAvSGufCP

and this previously asked question Error: "Selectors not implemented"

And fastreload is right about not dealing with the DOM in the controller.

http://docs.angularjs.org/guide/directive

Sign up to request clarification or add additional context in comments.

2 Comments

"fastreload is right about not dealing with the DOM in the controller" -- so here is a directive implementation
Mix jquery with angularjs is a bad practise. Try to use the element attribute in the link function of the directive to do this.This is not a right answer.

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.