Dynamic and operational icons accompany the AngularJS search input field

Currently, I am delving into the world of AngularJS and experimenting with a search box. Within this search box, I want to incorporate three icons: a magnifying glass, a cross, and a spinner. For the first scenario, I envision the magnifying glass appearing when the input box is empty. In the second scenario, I expect the spinner to display once the user has entered some characters into the input box, eventually leading to possible search results. As the search results are processed and presented, I anticipate the cross icon to emerge in the third scenario, allowing users to clear the field and start anew upon clicking.

Below is the progress I have made so far:

<div ng-app="searchDemo" ng-controller="LocationFormCtrl">
<div>
    <input type="text" class="clearable magnifying-glass" ng-click="search()"/>
    <!--<i class="glyphicon glyphicon-refresh spinning"></i>-->
        {{ outputText }}
    </div>

Check Out the Fiddle Here - Due to formatting issues, I have provided the remaining details in a separate fiddle.

I have also created a concise "clear field" example accessible through this link.

My main challenge lies in replacing the text representation of icons with actual icons inside the input box while ensuring they function effectively with Angular. Any assistance with this matter would be highly appreciated, as I have spent hours grappling with it and find myself increasingly perplexed. Your guidance is invaluable.

Many thanks, John

Answer №1

When working in HTML, remember to:

<input type="text" class="clearable" ng-model="searchText"/>
<i class="glyphicon" ng-class="searchIcon" ng-hide="clearEnable" ng-click="search()"></i>
<i class="glyphicon" ng-class="searchIcon" ng-show="clearEnable" ng-click="clear()"></i>

In the controller:

// Start with the magnifying glass icon
$scope.searchIcon = "glyphicon-search";

Within the search() function:

$scope.searchIcon = "glyphicon-refresh spinning";

And in the clear() function:

$scope.searchIcon = "glyphicon-search";

Check out the Updated Fiddle for a demo. You can also use ng-keyup or ng-blur events on the input tag.

Answer №2

To implement this functionality in Angular, you can utilize directives like ng-show and ng-keydown.

Check out this example for reference.


     <body ng-controller="tempC">
    <div class="input-group">
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-search" ng-show="search"></span>
        <span class="glyphicon glyphicon-pencil" ng-show="editing"></span>
        <span class="glyphicon glyphicon-remove" ng-show="remove" ng-click="removeIcon()"></span>
        </span>
      <input type="text" class="form-control col-sm-4" placeholder="enter" ng-model="text" ng-keydown="typing()" />
    </div>
  </body>


 angular.module('temp',[]).
 controller('tempC',['$scope','$timeout',function($scope,$timeout){
   $scope.remove = false;
   $scope.search=true;
   $scope.editing = false;
   $scope.typing = function(){
     $scope.search = false;
     $scope.editing = true;
     $timeout(operation,1000);

   }
   $scope.removeIcon = function(){
     $scope.remove = false;
     $scope.search = true;
     $scope.text = '';
   }
   function operation(){
     //perform operation

     $scope.editing = false;
     $scope.remove = true;
   }
 }])

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

The array becomes empty after a value has been assigned to it

I am currently working with a variable containing JSON data. var blogData = [ { "blogTitle":"Bangladesh", "imagePath":"/img/blog/bangladesh.jpg" },{ "blogTitle":"In ...

How to deactivate the mobile hardware back button functionality in Ionic 2

Our team has been developing a business application and we've encountered a perplexing issue. Every time we press the mobile hardware back button, our application's GUI becomes disrupted. Despite dedicating numerous hours to finding a solution, t ...

Running a series of functions consecutively with JQUERY

Currently, I am facing an issue with using an ajax method .load to replace the content of a div. The library I am working with does not allow me to replace the content of a div as some functions continue to run in the background. To overcome this challeng ...

Show flexibility in the grandchildren of a row layout

My goal is to achieve a layout similar to the image below using the flex "system", but I am facing a challenge with some generated directives that are inserted between my layout div and the flex containers: https://i.sstatic.net/nq4Ve.png <div id="i-c ...

Interacting with a JavaScript button using C# WebBrowser

Can someone please assist me with a problem I'm facing? I have a webpage coded in HTML and Javascript. Whenever I use webBrowser1.Document.GetElementById("hmenu").InvokeMember("click");, the menu pops up on my screen. However, I am unsure how to cli ...

How can I fill the data array of arrays using an Angular Table component?

I am struggling to populate an Angular Table with data in the array of arrays format. Despite my efforts, the code I have written does not seem to work. Any suggestions for a solution would be greatly appreciated. Here is the data that I am trying to popu ...

What initiates Electron after npm processes the package.json file?

As I delve into the realms of JavaScript, HTML, and Electron, a particular question has been playing on my mind - what exactly happens when you run electron . in the "scripts" -> "start" section of package.json? The mysterious way it operates sends shiv ...

What are the best circumstances to utilize jQuery-UI autocomplete feature?

Looking for assistance in creating an input field that auto-populates user-entered values with database entries. Only values that exist in the database should be accepted. Could someone explain the advantages of using jQuery-ui autocomplete for this task ...

Utilize a button to apply styles to a Span element dynamically generated with JavaScript

I am attempting to spin a span generated using JavaScript when a button is clicked. The class is added to the span, but the spinning effect does not apply. <p>Click the button to create a SPAN element.</p> <button onclick="myFunction ...

AJAX request: No values are being returned by $_GET

After spending hours trying to figure this out... I've been working on using AJAX to grab values from a jQuery slider within an <input> tag. The AJAX request is not failing (see code below), and when I use console.log to check the variable I&ap ...

enhanced labeling for checkboxes in Vuetify

Below is a snippet of my straightforward code: <v-checkbox v-model="rodo" label="I consent to Terms and Conditions (click for more info)" :rules="termsRules" required ></v-checkbox> I am looking to keep the label simple with an option ...

Can we retrieve the CSS of an element?

Using Selenium's webdriverJS, I have automated tasks on an HTML5 page. To incorporate a CSS selector into a function, I had to rely on XPath for selecting elements: var complexXpath = "//*/div/a"; /* This is just an example */ var element = mydri ...

What is the best way to calculate the duration of a .wav file stored locally using JavaScript?

Can you help me figure out how to determine the duration (in milliseconds) of a .wav file using JavaScript for a GreaseMonkey Script? The main challenges I'm encountering are: 1) Accessing local files 2) Retrieving the length of the wav file ...

Text that is selected within a contenteditable div: Find the beginning and end points of the highlighted text

<div contenteditable="true">This text is <b>Bold</b> not <i>Italic</i> right?</div> For instance, if the text appears in a bold format, but not italic and the user selects it, how can I determine the exact position ...

"Automatically insert a new row into the table if the cell loses focus and is not left

Can someone assist me with adding a table row dynamically when a cell is filled and the input focus is lost? My JavaScript skills are not strong. Here is a link to the form: JSFIDDLE <table class="table table-timesheet" ng-controller="TimesheetCtrl"> ...

The selected checkbox value does not take effect when used within an ng-if statement

Even though the ng-model value is present, it is not applied to the checkbox under ng-if. The update only occurs on double click. <div ng-show="isTrue == true"> <label> <input type="checkbox" name="test1" ng-model="a.testModel[0]" ng-true ...

Stopping the execution of code in Node.js after returning a JSON response

When a user is not found, the code still continues executing after sending the JSON response. The JSON response is generated in a separate class and returned from there. var user = new UserClass(obj, null); var userObj = user.getUser(res, req, 'user ...

Function for swapping out the alert message

I am searching for a way to create my own custom alert without interfering with the rendering or state of components that are currently using the default window.alert(). Currently working with React 15.x. function injectDialogComponent(message: string){ ...

Navigating to the most recent item within ng-repeat (AngularJS or JavaScript)

I am working on a feature where posts are displayed using ng-repeat in a div, and users can enter new posts in an input box. The posts are sorted so that the latest one appears at the bottom. After adding a new post, I want to automatically scroll down t ...

Troubleshooting: Dealing with Access-Control-Allow-Origin Issue

While debugging a web app in Visual Studio 2013 Express for Web, I encountered the following error. This particular app is built with MVC using angularjs, jquery, and bootstrap. Interestingly, my other web apps are functioning without any issues and do not ...