Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy focus on textbox is not set inside a twitter-bootstrap modal (popup) by AngularJS
    primarykey
    data
    text
    <p>I have been trying to implement what suggested here and other similar solution <a href="https://stackoverflow.com/questions/14833326/how-to-set-focus-in-angularjs/14837021#14837021">How to set focus on input field?</a></p> <p><a href="http://plnkr.co/WKq83K" rel="nofollow noreferrer">PLUNKER</a> my code with non-working autofocus.</p> <p><strong>HTML</strong> </p> <pre><code> &lt;body ng-controller='userNameController'&gt; &lt;button class="btn" id="enterUsernameBtn" href="#userNameModal" role="button" class="btn" data-toggle="modal" title="Enter Username" ng-click="focusInput=true"&gt;Enter Username&lt;/button&gt; &lt;!-- UserName Modal --&gt; &lt;div id="userNameModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="userNameModalLabel" aria-hidden="true"&gt; &lt;div class="modal-header"&gt; &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;×&lt;/button&gt; &lt;h3 id="userNameModalLabel"&gt;Enter your username&lt;/h3&gt; &lt;/div&gt; &lt;div class="modal-body"&gt; &lt;div class="input-append"&gt; &lt;input class="pull-left" id="userIdTextBox" type="text" ng-model="userName1" ng-minlength="1" ng-trim="true" focus-me="focusInput"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button class="btn" data-dismiss="{{whatToDismiss}}" aria-hidden="true" ng-click="submitUserName()"&gt;Submit&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p><strong>JavaScript</strong></p> <pre><code>var app = angular.module('abcApp',[]); app.directive('focusMe', function($timeout) { return { scope: { trigger: '@focusMe' }, link: function(scope, element) { scope.$watch('trigger', function(value) { if(value === "true") { $timeout(function() { element[0].focus(); }); } }); } }; }); app.controller('userNameController',function ($scope) { $scope.whatToDismiss=''; // workaround not to close the modal if it is an invalid input $scope.focusInput=false; $scope.submitUserName= function () { if($scope.userName1===undefined || $scope.userName1==="") { alert("Invalid Input"); return; } alert("username entered"+$scope.userName1); $scope.whatToDismiss='modal'; } }); </code></pre> <p>None of solution is working for me. I somehow get the focus to set on textbox whenever modal opens, but then I <strong>no more</strong> get value of <code>userName1</code> through <code>ng-model</code>. After implementing <code>focusMe</code> directive <code>userName1</code> is always undefined.</p> <p><strong>Edit</strong> </p> <p>Going to try this <a href="https://stackoverflow.com/questions/11896732/ngmodel-and-component-with-isolated-scope">Can I use ng-model with isolated scope?</a></p> <p>Because it seems <code>ng-model</code> won't work for above solution</p> <p>Meanwhile, anyone comes with answer to my question <strong>"How to set auto-focus to textbox inside twitter-bootstrap modal and able to get value entered in that textbox through ng-model"</strong> please share.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload