0

I create one input element and on click show the list of items after selecting any item list get hide.

But the focus is not on that element it get down,

So I just want to manually set the focus on input element when radio button list get hide.

Here is use the following code,

View

 <label class="item item-input item-stacked-label" ng-click="showDays()">
            <span class="input-label black-text">DAYS</span>
            <span class="input-ctrl">
            <input type="text" id="days" readonly  ng-model="data_day" focus-on="!daysflag">
            </span>
 </label>
 <div ng-show="daysflag">
          <ion-radio icon="ion-ios-checkmark" ng-model="data_day" ng-value="{{day}}" ng-repeat="day in days" ng-click="hideDayFlag()">{{day}}</ion-radio>
 </div>

Controller

        $scope.days = [1, 2, 3, 4, 5, 6, 7];

        $scope.showDays = function() {
          $scope.daysflag = true;
        }
        $scope.hideDayFlag = function() {
          $scope.daysflag = false;
        }

Directive

 .directive('focusOn',function($timeout) {
            return {
                restrict : 'A',
                link : function($scope,$element,$attr) {
                    $scope.$watch($attr.focusOn,function(_focusVal) {
                        $timeout(function() {
                            _focusVal ? $element[0].focus() :
                                $element[0].blur();
                        });
                    });
                }
            }
        });

Please help me to solve this issue.

1 Answer 1

1

You are wrong to use the function scope.$watch. scope.$watch is used to track changes in the $scope.

See detail explanation.

Example on jsfiddle.

angular.module('ExampleApp', [])
  .controller('ExampleController', function() {
    var vm = this;
    vm.isFocus = false;
  })
  .directive('focusOn', function() {
    return {
      restrict: 'A',
      scope: {
        focusOn: "="
      },
      link: function(scope, $element, $attr) {
        scope.$watch('focusOn', function(_focusVal) {
          _focusVal ? $element[0].focus() :
            $element[0].blur();
        });
        $element.on("focus", function() {
          scope.$applyAsync(function() {
            scope.focusOn = true;
          });
        });
        $element.on("blur", function() {

          scope.$applyAsync(function() {
            scope.focusOn = false;
          });
        })
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ExampleApp">
  <div ng-controller="ExampleController as vm">
    <input ng-model="vm.countRow" focus-on="vm.isFocus">
    <input type="button" value="Focus" ng-click="vm.isFocus=true">
    <input type="button" value="Blur" ng-click="vm.isFocus=false">{{vm.isFocus}}
  </div>
</div>

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

5 Comments

Thank you for your response but For the my issue its not working
i have issue when ng-show get false and list get hide then focus goes to directly on html input field
To solve your issue, you create directive focusOn. This directive doesn't work! You write So I just want to manually set the focus on input element when radio button list get hide. And i do it. Manually set focus on input element.
focusing is work but it not scroll up on that element
On chrome v49 scroll up on focus element.

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.