AngularJS - Introducing blurred delay

Here is the code snippet I am working with:

<div class="staff">

    <input ng-model="user" 
            ng-focus="isFocused = true;"
            ng-blur="isFocused = false;/>

    <div class="matches" ng-if="isFocused">
        <div class="user" 
            ng-repeat="match in users | filter:user" 
            ng-bind="match"
            ng-click="user = match">
        </div>
    </div>

</div> 

The directive is set on .staff. It contains an input field that displays matches while typing. The goal is to click a match and have it update the ng-model='user'.

Currently, this functionality does not work as intended. When focusing on the input, the matches quickly disappear, causing .matches to become hidden. This happens because once clicking on a match, the focus is lost on the input, setting isFocused to false.

To address this issue, my current solution involves using element.bind() within the directive to handle focus and blur events with a slight delay.

In my existing implementation, the input tag remains simple: <input ng-model="user"/>, and the link function of my directive looks like this:

link: function(scope, element, attrs)
{
    element.bind('focus', function()
    {
        scope.isFocused = true;
    });

    element.bind('blur', function()
    {
        $timeout(function() 
        {
            scope.isFocused = false;
        }, 125);
    });
}

Although this workaround functions properly, I would prefer to explore alternatives. Is there a better approach I can take?

Answer №1

<div class="team">
    <input ng-model="username" 
            ng-focus="isActive = true;"
            ng-blur="deactivate()"/>

    <div class="options" ng-if="isActive">
        <div class="member" 
            ng-repeat="option in choices | filter:username" 
            ng-bind="option"
            ng-click="username = option">
        </div>
    </div>
</div> 

link: function(scope, element, attributes){
    scope.deactivate = function(){
        $timeout(function(){
            scope.isActive = false;
        }, 125);
    });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Receiving the final outcome of a promise as a returned value

Seeking to deepen my comprehension of promises. In this code snippet, I am working on creating two promises that will return the numbers 19 and 23 respectively. However, when attempting to console log the value returned from the first promise, I encounte ...

The functionality of 'Access-Control-Allow-Origin': '*' is not operational

I attempted to address all inquiries pertaining to this tag, yet I encountered a hurdle. Where did I go wrong? $(document).ready(function () { $.ajax({ type: "GET", url: "http://www.tcmb.gov.tr/kurlar/today.xml", dataType: "xml ...

Issue with floating navigation bar functionality when resizing the page

After much tinkering, I have managed to create a floating navigation bar for my website. Most of the time, it works perfectly fine, but there's an issue when the window size is adjusted in the floating mode. Take a look at the image below for referenc ...

Struggling to make ng-repeat function properly within an AngularJS controller when working with arrays

I am having trouble with my code. The ng-repeat in my AngularJS app is displaying blank. Can someone help me figure out why? Thanks in advance, Stephanie <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/aja ...

Guide to populating a full calendar using JSON information

Implementing the FUllCALENDAR CSS template for creating a meeting calendar has been my current project. The servlet class I am using is CalendarController. However, when running it, the output appears as follows: {"events":[{"id":1,"title":"1","s ...

Error: Property 'html' is undefined - AJAX response must be displayed only in a specific div

I encountered an issue: Uncaught TypeError: Cannot read property 'html' of undefined I am working on implementing a voting system for each video on my webpage from a database. I have successfully integrated it using AJAX, but the problem is ...

What is causing my fetch response to not be passed through my dispatch function?

I'm currently utilizing a node server to act as the middleman between firebase and my react native app. Could someone kindly point out what might be going awry in my fetch method below? export const fetchPostsByNewest = () => { return (dispatch ...

Determine the value of an object by iterating through its keys

UPDATE: (for clarification) I currently have a table named modelCoa +----+----------+-------+--------------------+ | id | id_parent| code | name | +----+----------+-------+--------------------+ | 1 | 0 | 1 | asset ...

Simulating Cordova plugin functionality during unit testing

I have a code snippet that I need to test in my controller: $scope.fbLogin = function() { console.log('Start FB login'); facebookConnectPlugin.login(["public_profile", "email", "user_friends"], FacebookServices.fbLoginSuccess, FacebookServic ...

Guide on removing query parameters post a redirect using NextJS

Is there a way in NextJS to redirect a URL like /page?foo=bar to /page/bar? I checked out the documentation at https://nextjs.org/docs/api-reference/next.config.js/redirects but couldn't find a solution. This is what I have tried so far: { source: ...

Injecting dependencies through the Angular factory method allows for seamless

I'm in the process of transferring some standard navigation formatting code from the controller to a factory. Should I include $scope in my factory? Despite numerous attempts, I have been unsuccessful in injecting $scope using various methods. Do my ...

firebaseArray is returning a size of zero

Seeking assistance in determining the array length from Firebase, I have implemented the following code: (function() { 'use strict'; angular .module('com.td.oca.tDUITestHomepage') .controller('HomepageCont ...

Submitting an AJAX request to upload an XML file and an image file simultaneously in one call

I need to send both an uploaded image and an uploaded XML file to a PHP file using a single AJAX call. I have tried using two form data instances, but I am not sure if this is the correct approach. <input type="file" class="form-control-file" name="fil ...

Choose a division of an element within an embedded frame

Two different pages on the same website, Home.html and Page2.html. Home.html contains the following code: <html> <iframe id="one" src="page2.html" style="display:none"></iframe> <div id="container"> <h1& ...

Change the text inside a container without losing any associated event listeners using JavaScript or jQuery

Here is the HTML code: <div id="div001"> This is ${Row.1}. <p>${Row.2} explain something else.</p> <p>${Row.3} welcome you. <span>${Hello.World}, this is a new world.</span></p> </div> My objective is ...

The side navigation panel transition is stuck and failing to slide out as intended

My element is able to slide in smoothly, but encounters trouble when attempting to slide back out. I suspect the issue lies within the syntax for "display: none". Any assistance or recommendations would be greatly appreciated, and feel free to request more ...

How can I temporarily turn off the animation on a directive in Angular JS to prevent it from animating during page load?

Check out this JSFiddle for the issue: http://jsfiddle.net/goodwill/ezNuj/ I have created a custom directive with the code below: myApp.directive('ngFadeIn', function() { return function(scope, element, attr) { if (element.is('tr, t ...

Modifying the color of a header through clicking on a specific anchor link

My goal is to create a fixed side nav bar on my webpage with an unordered list that links down the page using anchors. In addition, I want to dynamically change the color of an h2 element based on which item in the fixed nav bar is clicked on. For instan ...

Using jQuery, you can utilize the $.when() function with both a single deferred object and an

In my current jquery setup, I am working with two variables. trackFeatures - representing a single ajax request, and artistRequests - an array of ajax requests. I am looking for a way to create a condition that triggers when both trackFeatures and artist ...

What is the best way to close all other accordion tabs when selecting one?

A simple HTML code was created with the full pen accessible here. <div class="center"> <div class="menu"> <div class="item"> <button class="accordionBtn"><i class=&q ...