Sean256 Sean256 - 4 months ago 59
AngularJS Question

ngClick but prevent any active inputs from losing focus

In my Angular view I have an input field and some clickable goodies using ng-click.

Is there an angular way or simple vanilla js way to prevent the input field from LOSING focus (if it was in focus) when one of these ng-clicks is clicked?

<input type="text" class="form-control" ng-model="foo">

<button ng-click="someThing()">click me</button>


Not with ng-click. But ng-mousedown should work, if you $event.preventDefault() in your click handler. Here's a demonstration in vanilla JS; the same should work in Angular:

    var preventBlur = function(event) {
    <input type='text' onblur="alert('blurred')">
    <button onmousedown="preventBlur(event)">Click</button>

If you click the button while the input field is in focus, the mousedown will trigger without causing the input field to lose focus.