What is causing the Angular-UI TypeAhead code to display all items instead of filtered items?

I have been experimenting with the angular-ui typeahead directive to create a filtered input box that only shows items based on what has been typed. However, my current code is displaying all the items instead of just the filtered ones.

If you'd like to take a look at the code, I have created a plunker which can be found here.

I would greatly appreciate any guidance on what might be wrong with my implementation. Thank you!

In case you are unable to access the plunker, here is a snippet of the relevant HTML:

<div class='container-fluid' ng-controller="TypeaheadCtrl">
    <h4>Testing angular-ui Typeahead</h4>
    <input type="text" ng-model="typeahead" typeahead="names for names in getName($viewValue) " class="form-control">    
</div>

And here is the relevant JS code:

function TypeaheadCtrl($scope, $http) 
{
  // Function to load values asynchronously  
  $scope.getName = function(val) 
  {
    return $http.get('test.json') 
    .then(function(res)
    {
        var names = [];
        angular.forEach(res.data, function(item)
        {
            names.push(item.name);
        });
        return names;
    });
  };
}

The json file obtained from the http request looks like this:

[
{
    "name": "Tom"   
},
{
    "name": "Tom2"  
}
]

Answer №1

When you consistently return an unfiltered array, it is most likely that this can be handled on the server side. However, if the array is static, you can achieve this by following the method below:

http://plnkr.co/edit/kxOlmnjGA7wX7zhS67aK?p=preview

angular.module('plunker', ['ui.bootstrap']);

function TypeaheadCtrl($scope, $http) {
  // Any function returning a promise object can be used to load values asynchronously  
  $scope.getName = function(val) {
    return $http.get('test.json')
      .then(function(res) {
        var names = [];
        angular.forEach(res.data, function(item) {
          if (item.name.toLowerCase().indexOf(val.toLowerCase()) > -1) {
            names.push(item.name);
          } else {
            console.log(item);
          }
        });
        return names;
      });
  };
}

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 design a bookmarklet that can overlay an HTML/div layer and apply CSS styles from an

Is there a way to incorporate a bookmarklet that can bring in a new layer/div with additional HTML and CSS from an external file, overlaying it on the current page? If anyone has an example of a bookmarklet that achieves this, would you be willing to shar ...

The parsing of source maps fails due to issues with the .htaccess file

After analyzing the web page, I found that the .htaccess file contains the following code: RewriteEngine On RewriteBase / Options -MultiViews DirectorySlash Off # skip POST requests RewriteCond %{REQUEST_METHOD} POST RewriteRule ^ - [L] RewriteCond %{R ...

Enhancing ReactJS functionality by incorporating custom logic prior to resolving promises

In one of my components, there is a function as follows: this.props.firebase.getDropSites("123456").then(data => { console.log(data); }); This function in turn calls the following method from my utilities class: getDropSites(dropSiteId) { return th ...

Tips for incorporating a Forgot/Reset password option into your #Firebase platform

In the project I'm working on, I am utilizing #AngularFire2. My goal is to incorporate a Reset / Forgot password link into the login page. Does anyone have suggestions on how to accomplish this task? I'm looking to get some insights from #AskFi ...

Tips for transferring data from a kendo grid to an Excel spreadsheet while maintaining multiple lines in the header cells

How can I ensure that the header cell in my grid displays multiple lines of text at specific positions, both visually and in an exported Excel worksheet? Currently, the text wraps in the grid but does not export as multi-lines to Excel. Is there a way to r ...

Expanding Headers with JavaScript

Looking to add a Stretchy Header Functionality similar to the one shown in this GIF: Currently, on iPhones WebView, my approach involves calling a Scope Function On Scroll (especially focusing on Rubberband Scrolling) and adjusting the Image Height with C ...

Error in AngularJS when passing object to modal dialog

I'm facing a challenge with an AngularJS application I'm developing. It involves displaying a list of contacts, each accompanied by a button that triggers a modal containing a form for editing the contact information. The issue arises when attemp ...

Custom positioning of Mui Snackbar in V5

I've been attempting to position a Snackbar in the top right corner with some customization for the top property, but I'm struggling to get it to display correctly. Here's what I've tried: import React from "react"; import { ...

Using jQuery to remove all inline styles except for a specific one

Just a quick inquiry: Our Content Management System (CMS) utilizes CKEditor for clients to modify their websites. The front-end styles include the use of a pre tag, which we have customized to our desired appearance. However, when their team members copy a ...

Improved Approach for Replacing if/else Statements

I'm looking to streamline the controller used in my SQL command for filtering records based on specific criteria. The current approach below is functional, but not without its limitations. One major issue is scalability - adding more criteria in the f ...

Color the column of our kendo ui grid in gray

Within this kendo ui grid here, the initial column [OrderID] cannot be modified. I am seeking a solution to visually distinguish all disabled columns by applying a subtle gray shade, allowing users to easily identify them as non-editable. ...

Enabling real-time notifications through Express 4 middleware with socket.io integration

I am in the process of developing a real-time notification system utilizing socket.io. Here is the current server-side code I have implemented: bin/www: var app = require('../app'); var server = http.createServer(app); var io = app.io io.attac ...

Disseminate several outcomes using a Discord bot

My first experience using stackoverflow was to seek help regarding a bot created to post results whenever a new episode of a show in the search list is added on nyaa.si. The issue I'm facing is that the bot posts the same episode multiple times within ...

jQuery Issue - Clash between multiple menus sharing the same class

Hey there! I'm currently diving into the world of jQuery and encountering an issue with my menu. Whenever I click on a menu item with either the "tm-nav-vertical" or "tm-nav-horizontal" class, it removes the .active class from the initial menu item. I ...

Retrieving the value of an array from a JSON data structure

I am working with the object shown below to extract the desired output. The result will be a new object that represents the final output. var data = { Customer: { Name: "emp1", Departments: [ {Departme ...

javascript: revealing the identity of a click event handler

Here I am looking to create a click function with a specific name and parameters for the purpose of code reusability. This will allow me to write one generic function that can be used for common tasks like enabling users to delete various types of data. I ...

I have a website hosted on Heroku and I am looking to add a blog feature to it. I initially experimented with Butter CMS, but I found it to be too pricey for my budget. Any suggestions on

I currently have a website running on Heroku with React on the front end and Node.Js on the back end. I want to incorporate a blog into the site, but after exploring ButterCMS, I found the pricing starting at $49 to be too steep for my budget. My goal is ...

Is react-particles-js still compatible for me to integrate?

I recently discovered that the package found here: https://www.npmjs.com/package/react-particles-js has been deprecated. Can I still utilize this package? The codes in question can be viewed at: https://codesandbox.io/s/particle-js-background-forked-woypk ...

Retrieving information within the iteration

I am facing an issue with connecting to an external server named Pexels in order to retrieve photos from node.js. The problem seems to be related to JavaScript, as Pexels limits the user to download up to 40 pictures per page. https://api.pexels.com/v1/cu ...

Is there a way for me to view the properties within the subcomponents?

Working on a project to create a bulletin board using React, following the official documentation. Decided to consolidate all actions related to the bulletin board into one alert component called AlertC. In the Form onSubmit statement, if the title is tr ...