Mark Mark - 1 year ago 73
AngularJS Question

Focus on input field

Template is 'tabs' with 2 tabs. On the first tab, I have an

field I want to put focus on. It is working when the app starts with the
field being focused, but when I switch to the second tab and then back to the first tab, the input loses focus.

I would like it be focused when I go from tab 2 to 1 too. When I click outside the element, it to loses focus as well. I actually want that
field to be ALWAYS focused.

I tried some already mentioned solutions:

.directive('focusMe', function($timeout) {
return {
scope: { trigger: '=focusMe' },
link: function(scope, element) {
scope.$watch('trigger', function(value) {
if(value === true) {
//$timeout(function() {
scope.trigger = true;

<label class="item item-input" focus-me>
<input type="number" class="somett" ng-model="code" ng-model-options="{ debounce: 100 }" placeholder="Ready" ng-change="load(code) " focus-on="focusTextInput" autofocus>

Answer Source

You can use View LifeCycle and Events.

Add this to your controller.

$scope.$on('$ionicView.enter', function() {
    //trigger input focus