Closing the autosearch view in AngularJS is a simple process that can enhance the

I just started learning angularjs and I'm struggling with closing the search results when clicking on the "Close search result" span. Below are snippets of my code. Can someone please guide me on how to achieve this functionality?

Screenshot:

HTML:

<div>
    <div>
        <input type="text" placeholder="Choose a item..." ng-model="autoSearch" ng-keyup="getList()" class="form-control">
    </div>
    <div class="autosearch-result">
        <div class="search-result-close-block">
            <span class="search-result-close-text">Items found :</span>
            <span class="close" title="Close search result">×</span>
        </div>
        <ul>
            <li>.....</li>
            <li>.....</li>
            <li>.....</li>
        </ul>
    </div>
</div>

CSS:

.autosearch-result {
position: absolute;
top: 100% !important;
z-index: 1;
}

Answer №1

Make sure to include a boolean value in your scope to control the visibility of the span element.

Utilize the ngShow directive to toggle the display of the div.

<div>
    <div>
        <input type="text" placeholder="Choose an item..." 
              ng-model="autoSearch" ng-keyup="getList()" class="form-control">
    </div>
    <div class="autosearch-result" ng-show="!displayResult">
        <div class="search-result-close-block">
            <span class="search-result-close-text">Items found :</span>
            <span class="close" title="Close search result" 
                  ng-click="displayResult = false">×</span>
        </div>
        <ul>
            <li>.....</li>
            <li>.....</li>
            <li>.....</li>
        </ul>
    </div>
</div>

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

What is the best way to assign a series of radio buttons to an array within an Angular controller's model?

Let's say I have a controller that contains an array property named 'houses'. I want to use ng-repeat to display this array on a table row with a set of radio buttons (true/false, etc.). How can I ensure that selecting any of these radio but ...

With Ionic, you can use one single codebase for both iPad and iPhone

I currently have a complete app developed using ionic and angularjs that is functioning well on iPads and Android devices. Now we are looking to launch it for iPhones and Android smartphones with some design modifications. Is there a method to achieve th ...

ng-model in html input with a name of "foo[]"

Apologies for the lack of a more specific title. In HTML, when we need multiple values for a given name, we utilize the name="foo[]" attribute. Upon posting the data, it arrives as an array. I am seeking this same functionality with ng-model in Angular. ...

Retrieving InnerHTML of a Rendered DOM Element in AngularJS

Can I retrieve the innerHTML code of a rendered element that contains an ng-repeat loop? Here is an example: <div id="container"> <div ng-repeat="e in ctrl.elements>{{e.name}}</div> </div> ...

Assigning null values, or initializing values in AngularJS

How can I clear input values to null after clicking on a button? Empty fields: Fields with existing values that I want to reset back to empty fields. HTML: <label class="item item-input"> <input id="tezina" type="number" placeholder ...

Harnessing the power of custom directives in HTML

I have developed a custom directive that adds the active class to the clicked li element in the menu list based on the URL. .directive('addActive', [function() { return{ ... link : function(scope, element, attrs){ ...

Exploring the depths of JSON using @attributes and @association in the realm of JavaScript and AngularJS

Currently, I am working on a project that involves utilizing an API for data retrieval, updates, and deletions. The API in question is the prestashop API. While I have managed to retrieve data and update certain items successfully, I encountered an issue. ...

Converting an array into an object using Typescript and Angular

I have a service that connects to a backend API and receives data in the form of comma-separated lines of text. These lines represent attributes in a TypeScript class I've defined called TopTalker: export class TopTalker { constructor( pu ...

Looking to replace the cursor on your computer?

Upon exploring the angular-ui-tree, I observed that items are equipped with a drag or move cursor, which is helpful for making items draggable. However, I am curious about how to change the cursor to a normal arrow instead. Can anyone provide guidance on ...

Utilizing Angular Controllers to Access HTML Elements without the Need for jQuery

Within my Controller, I am trying to access an html element that is generated automatically by an external library on the DOM. Unfortunately, importing a directive inside the html is not an option in this case. So, my question is: Is there a way to retrie ...

Leveraging $compile within a directive

My directive is designed to only execute when the $compile.debugInfoEnabled() method returns true. The issue I am facing is that the $compile object is showing up as undefined: angular .module('myapp', []) .directive('myDebugThing& ...

AngularJS: resolving route dependencies

I have a variable $scope.question that contains all the questions for the page. My goal is to loop through the questions page by page. To achieve this, I created a function called questionsCtrl and I am calling this function in the config while setting up ...

What are the steps to modify the authorization header of an HTTP GET request sent from a hyperlink <a href> element?

I have a unique Angular application that securely saves JWT tokens in localstorage for authentication purposes. Now, I am eager to explore how to extract this JWT token and embed it into an HTTP GET request that opens up as a fresh web page instead of disp ...

What strategies can I use to refactor this controller in Angular 1.5 to make it more concise and efficient

I am encountering an issue with a component I have that contains a button. Whenever the button is clicked, it triggers one of two backend services depending on where the component is located. To achieve this, I am currently passing a flag to the component ...

What is the process for including item prices in Angularjs?

I have a question regarding adding work item costs and encountering issues with displaying the original values. Here's an example: item[1].cost = 2, item[2].cost = 2 .. When I add the cost of the 3rd item (item[3].cost = 8), the total shows as 228. ...

Separate and add on the change validity status of an AngularJS form

If you want to test it out, check this jsFiddle link: HERE (it's recommended to view on a new jsFiddle, refer to the EDIT section of this post) I've noticed what seems like a bug in AngularJS or at least an unexpected behavior. When I detach a f ...

Updating the `link` function to target a specific DOM element within an Angular 2 component

Angular 1 uses the link function in a directive to target DOM elements. link: function (scope, element, attr) { // do something with element[0], e.g. put generated graphics // inside the node } What is the equivalent feature in Angular 2? ...

Utilizing AngularJS to access the corresponding controller from a directive

When I have HTML structured like this... <div ng-app="myApp"> <div ng-controller="inControl"> I enjoy sipping on {{beverage}}<br> <input my-dir ng-model="beverage"></input> </div> </div> a ...

My AngularJS controller is failing to display database data in the routed view

Recently delving into AngularJS, I've been attempting to fetch data from a MySQL database for one of my routed pages. Despite consulting various tutorials online, I seem to be missing something and can't pinpoint the issue. INDEX PAGE: <!DOC ...

Unable to access external library using browserify and debowerify

I'm facing a dilemma with my current setup as I'm dealing with a headache. Here's how things are currently configured: Utilizing bower to acquire vendor libraries (specifically angular) Executing gulp tasks to run browserify Implementing d ...