1
**HTML**

<div ng-app="myApp" ng-controller="myCtrl">
    <input placeholder="Enter your teams name" ng-model="team_input" ng-show="myVar" ng-focus="true" class="team_input"  ng-class="{redPlaceholder : newteamfocus, whitePlaceholder : !newteamfocus}" ng-blur="newteamfocus=true" type="text" name="team_input" autofocus required/>
     <button ng-click="toggleTeamField()">Add New</button>
</div>


**JS**

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.myVar = false;
    $scope.toggleTeamField = function() {
        $scope.myVar = !$scope.myVar;
        $scope.newteamfocus = false;
        $scope.team_input = "";
    };
});

Here I can show hidden input box, but the blinking cursor is not coming, when I clicked on add button. How to show Blinking cursor in input box. Plunker

m4n0
  • 29,823
  • 27
  • 76
  • 89
Naveen
  • 757
  • 3
  • 17
  • 41

1 Answers1

2

You can achieve this using a directive which focuses the element on click.

http://plnkr.co/edit/rrjbCtrhxSkG0oRTL0TU?p=preview

One important thing to note is the element needs to be visible to focus. So add a timeout for 0.

.directive('myFocus', function($timeout) {
  return function(scope, element, attrs) {
    attrs.$observe('myFocus', function() {
      if (scope.$eval(attrs.myFocus)) {
        // Element needs to be visible to focus
        $timeout(function() {
          element[0].focus();
        })
      }
    })
  };
});

//html
<input placeholder="Enter your teams name" my-focus="{{focus}}" ng-model="team_input" ng-show="myVar"  class="team_input"  ng-class="{redPlaceholder : newteamfocus, whitePlaceholder : !newteamfocus}"  type="text" name="team_input" required/>
Bharat
  • 943
  • 7
  • 9