Disregard the ng-blur event for specific elements

In my search feature, I have a suggestion box that appears below the search field when I start typing. I want the suggestion box to disappear when I click outside the search field, and also be able to click on a suggestion to perform a search.

<input ng-blur="suggestionBox.hide()" ng-model='someModel' />

<div class='suggestion-box'>
<ul>
<li ng-repeat="suggestion in suggestions" ng-click="someFunction()">suggestion.name</li>
</ul>
</div>

The issue I am facing is that the ng-click is not triggered when the ng-blur hides the suggestion box. I have tried using

ng-click="$event.preventDefault(); someFunction()"
but it did not work. I also attempted to set a timeout on the hide() method, which worked but required a long delay of 200ms, making it an inefficient solution.

edit: http://plnkr.co/edit/9BR7Sv2502S87HO56Ofp?p=preview

Answer №1

My approach involves utilizing ng-mousedown and ng-mouseup functions to handle click events without relying on onBlur. This technique was particularly useful when implementing a cancel edit button.

  1. When mousedown occurs, I set a flag "inWhateverMode = true". This step is crucial as it happens before the blur event.
  2. In the blur handler, I check for the presence of inWhateverMode.
  3. Upon mouseup, I clean up any flags and carry out the desired action.

This method proves beneficial in scenarios where users need to click a button, move the cursor away, and release without unintended consequences, such as accidentally triggering a delete action.

Check out the updated Plunk for reference: http://plnkr.co/edit/raJzMor9ci8dLgzXuQII?p=preview

ng-mousedown='startToShowItem(item)' ng-mouseup='show(item)'

Answer №2

Hey there, I believe I have found the solution you were seeking:

http://example.com/edit/3hjgij45hjgjhf

Is the issue that the function executes, but the drop-down menu disappears afterwards?

$scope.display = function(element) { $scope.hideElement = true; alert(element); };

Updated HTML:

<input ng-model='search' />
<div ng-hide='hideElement' style='border:1px solid black'>

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

Harness the power of electrons with the simple push of a button - initiating program execution

Recently, I delved into Electron with the desire to create a small application for myself. This app would allow me to run different programs or games by simply clicking on a link. My goal is to have the program/game start automatically when I open the Ele ...

Issue with Socket.IO: socket.on not executed

Recently, I devised a custom asynchronous emitter for implementing a server -> client -> server method. Regrettably, the functionality is not meeting my expectations. Although it emits the event, it fails to execute the callback as intended. Upon a ...

Trouble getting Fontawesome icons to accept color props when using react functional components with tailwindcss

Issue I'm Facing I'm currently working on a project that involves using icons extensively. Instead of manually adding a Fontawesome icon in every script, I have created a functional component that handles the rendering of icons based on given pr ...

I struggled to modify the image cropping code to specify a particular image

(I will attempt to explain my issue once again) I came across a script online which can be viewed at this link : Link However, I am having trouble modifying the code to suit my needs. The script currently starts working on image upload, but I want it t ...

Utilizing Cordova and AngularJS to efficiently retrieve JSON data through XMLHttpRequest in a factory

I've encountered an issue while attempting to retrieve a JSON from an API using XMLHttpRequest within a factory. Despite the debug logs suggesting that everything is functioning correctly, I keep getting an "undefined" response. Below is the code for ...

View the selected radio buttons and checkboxes next to each other in real-time before finalizing and submitting

Within my form, individuals have the option to select from radio buttons and checkboxes. The challenge is that I need to display the chosen data on the page before they enter their email and submit! Since I cannot refresh the page, PHP won't work for ...

Storing data in localStorage and serializing it using JSON.stringify and

Currently, I am developing a project that enables users to share memories of places they have visited and also tracks the location and time when the memory was recorded. One hurdle I'm facing involves incorporating localStorage into the application. I ...

Incorporating groovy script into HTML: Is it possible, akin to embedding JavaScript

Can groovy script be embedded in HTML similar to JavaScript? I am interested in creating an onclick button event that utilizes groovy script instead of JavaScript. Thank you. ...

Java Entity Framework Indexing Tables

I am currently utilizing ASP.Net Core and have implemented EntityFramework to create a controller with views. Specifically, I am in the process of enhancing the Index view to make it dynamic with dropdown selections. I have successfully completed everythin ...

What is the best way to retrieve the root binding node from a viewmodel in order to apply jQuery.blockUI when performing an AJAX post request?

Within my code, I have a designated DIV element that serves as the root node for applying knockout bindings like so: ko.applyBindings(viewModel, document.getElementById('myContainerDiv')); In all of my viewmodel types, there is a generic post m ...

Whenever the 'something' is passed in $state.go, it fails to function as intended

When using my app, I needed to check the current user so I wrote boot code for it. However, there seems to be an issue with the controller not loading when passing something in the URL. /* * boot controller */ .controller('BootCtrl', functio ...

Find the string "s" within a div element aligned vertically, using Popper

Currently utilizing Popper from Material-UI <Popper id={"simplePopper"} open={true} style={{backgroundColor: 'red',opacity:'0.5',width:'100%',height:'100%'}}> <div style={{height:"100%", ...

Retrieve an element within a jQuery each loop

I'm currently implementing AJAX functionality to retrieve cart items from the server and display them within a cart when a customer clicks on the "My Cart" button. Here is the model for the cart: public class Cart { [Key] public i ...

What could be the reason behind the failure of JSON.stringify() on this particular array?

I am encountering an issue with an array that I have declared like this: array = []; The array contains values that look like this - .... ChIJOaegwbTHwoARg7zN_9nq5Uc:"ChIJOaegwbTHwoARg7zN_9nq5Uc" ChIJXTwCdefHwoAR9Jr4-le12q4:"ChIJXTwCdefHwoAR9Jr4-le12q4 ...

Having difficulty resolving all parameters for the component: (?, [object Object]) in the Jasmine component Unit Test

While defining a UT for a component with an extended class using i8nService and ChangeDetectionRef, I encountered an error preventing me from instantiating it: Failed: Can't resolve all parameters for BrandingMultiselectComponent: (?, [object Object] ...

Clearing the ng-model value in a controller following an ng-change event

Is there a way to reset the ng-model value in the controller after an ngChange event without needing to use a directive? <div ng-repeat="i in items"> <!-- Some DOM comes here --> <select ng-model="i.avail" ng-change="changeAvail(i. ...

Is there a way to arrange an array based on the initial value?

In my array, I have a mixture of strings and arrays. Each string corresponds to the array next to it. For example, the string 789 is associated with the array ['111A', '222B', '333C']. My goal is to sort based on the strings w ...

Displaying the specified item within an array using AngularJS

My application features a group of 'players' each with their own unique attributes that I want to showcase within a modal window and cycle through. The user interface is structured like this: <!-- Score Round Modal --> <div class="mo ...

How to resolve CORS error when using custom request header in Django with JavaScript and redirecting to OPTIONS request?

I am currently working on building an API library using Django. This API will be accessed by javascript, with the Django-API and javascript running on separate servers. The Django API library requires a custom request header from the javascript front end, ...

Utilizing UI Grid 3.0 to effectively sort and display boolean values

How can I filter boolean values using ui-grid 3.0? The tutorial did not provide instructions on how to filter values with fields other than a text field. Is it possible to use a checkbox or standard HTML select for boolean values instead? ...