Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - ng-if a field is focused

I have the following HTML:

<input class="user" ng-model="user" />
<div class="matches">
    <div class="match" ng-repeat="match in matches | filter:user" ng-bind="match"></div>
</div>

I only want to show .matches IF you are typing and searching for a user. Otherwise, I don't want to show the field. But the input field is always displaying the user (so I can't use the fact that the input is empty or not).

Is there a way I can use the fact that the input is focused directly? Right now, I manually update a value ng-if="isFocused" when you focus in and out.

like image 931
Kousha Avatar asked Dec 01 '22 01:12

Kousha


2 Answers

<input type="text" ng-focus="focused = true" ng-blur="focused = false" />

<p ng-if="focused">focused!</p>

Plunker

like image 51
Andy Gaskell Avatar answered Dec 09 '22 22:12

Andy Gaskell


check this

<input class="user" ng-model="user" />
<div class="matches">
    <div class="match" ng-if="user!=''" ng-repeat="match in matches | filter:user" ng-bind="match"></div>
</div>
like image 41
rjdmello Avatar answered Dec 09 '22 21:12

rjdmello