0

Hello I am trying ng_focus on click button like

Input Box Code:

 <input type="text" placeholder="" class="promocode"    ng-focus="focus"  ng-model="text"  value="" /> 

Button Code:

<span ng-click="text = 'sheetesh'; focus=true"> Sheetesh </span>
<p>focus: {{focus}}</p>

And Its return true click on span but focus is not work.

focus: true

Screen Shot: its showing like that

borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
RITU
  • 279
  • 3
  • 6
  • `ng-focus` is not for setting focus. It's like `ng-click`. Value of `ng-focus` will be executed when focus event occurs. – Egan Wolf Jan 08 '16 at 07:11

2 Answers2

3

document.getElementById

Try this:

<input id="myInput" type="text" placeholder="" class="promocode" ng-model="text" value="" />

<span ng-click="text = 'sheetesh'; document.getElementById('myInput').focus();"> Sheetesh </span>

angular.element

Alternative version with angular.element:

<span ng-click="text = 'sheetesh'; angular.element('#myInput').focus();"> Sheetesh </span>
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
Egan Wolf
  • 3,533
  • 1
  • 14
  • 29
0

you not need to set focus. just add 'tabindex' attribute to element then automatically focus will go on element on

<input id="myInput" type="text" placeholder="" class="promocode" ng-model="text" value="" />

<span ng-click="text = 'sheetesh';" tabindex="0"> Sheetesh </span>

onclick

saikumar
  • 1,041
  • 6
  • 10