I have a template that is loaded in a modal dialog, there is a single input text on it which I would like to have a focus on. What is the easiest and the Angular way of doing that?
Update:
This is in part answered in How to set focus on input field?
AngularJS ng-focus DirectiveThe ng-focus directive tells AngularJS what to do when an HTML element gets focus. The ng-focus directive from AngularJS will not override the element's original onfocus event, both will be executed.
To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.
Depending on the browser(s) you need to support you could use input autofocus attribute.
See plunker here.
Or if this is not an option and has mentioned by Mark you can refer to the following stackoverflow response.
How about this?
HTML
<div ng-controller="ctrl as vm" ng-init="vm.focus();"> <form name="Form"> <input type="text" id="input1"> <input type="text" id="input2"> </form>
JS
angular.module('app', []).controller('ctrl', function() { var vm = this; vm.focus = function() { document.getElementById('input2').focus(); }; });
https://plnkr.co/edit/noLUjVjycpnezpvEIgw7?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With