Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ng2-completer

oferh72.5kMIT9.0.1TypeScript support: included

ng2 autocomplete/typeahead component

angular2, autcomplete, typeahead, word completion, ng2

readme

ng2-completer

Auto complete component for Angular 2.

This component is based on angucomplete-alt

Click for demo or plunk

Help needed

I don't use this library much anymore and don't have time to properly maintain it. If you are currently using ng2-completer and interested to maintain it please let me know!

Installation

npm install ng2-completer --save

Usage

The module you want to use ng2-completer in must import Ng2CompleterModule and FormsModule (to use the ngModel directive on ng2-completer). Ng2CompleterModule provides the CompleterService, and declares the ng2-completer directive.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { AppComponent } from './app.component';
import { Ng2CompleterModule } from "ng2-completer";


@NgModule({
  imports: [
      BrowserModule,
      FormsModule,
      HttpClientModule,
      Ng2CompleterModule,
  ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Add ng2-completer to your component and create a data source:

import { Component } from '@angular/core';
import { CompleterService, CompleterData } from 'ng2-completer';

@Component({
  selector: 'my-component',
  template: `<h1>Search color</h1>
            <ng2-completer [(ngModel)]="searchStr" [datasource]="dataService" [minSearchLength]="0"></ng2-completer>
            <h1>Search captain</h1>
            <ng2-completer [(ngModel)]="captain" [datasource]="captains" [minSearchLength]="0"></ng2-completer>`
})
export class MyComponent {

  protected searchStr: string;
  protected captain: string;
  protected dataService: CompleterData;
  protected searchData = [
    { color: 'red', value: '#f00' },
    { color: 'green', value: '#0f0' },
    { color: 'blue', value: '#00f' },
    { color: 'cyan', value: '#0ff' },
    { color: 'magenta', value: '#f0f' },
    { color: 'yellow', value: '#ff0' },
    { color: 'black', value: '#000' }
  ];
  protected captains = ['James T. Kirk', 'Benjamin Sisko', 'Jean-Luc Picard', 'Spock', 'Jonathan Archer', 'Hikaru Sulu', 'Christopher Pike', 'Rachel Garrett' ];

  constructor(private completerService: CompleterService) {
    this.dataService = completerService.local(this.searchData, 'color', 'color');
  }
}

ng2-completer uses rxjs stream as data sources. There are 2 ready made data sources that can be used to fetch local and remote data but it's also possible to provide a custom source that generates a stream of items.

System.js configuration

Add the following to System.js map configuration:

   var map = {
       ...
       'ng2-completer': 'node_modules/ng2-completer/ng2-completer.umd.js'
   }

API

ng2-completer component

Attribute Description Type Required Default
datasource Autocomplete list data source can be an array of strings or a URL that results in an array of strings or a CompleterData object Array<string>|string|CompleterData Yes
dataService Deprecated use datasource instead. Autocomplete list data source. CompleterData Yes
ngModel see the angular forms API. string Yes
autoMatch Auto select an item if it is the only result and it is an exact match of the search text. boolean No false
autofocus Set input focus when the page loads boolean No false
clearUnselected Clear the input on blur if not selected. boolean No false
clearSelected Clear the input when a result is selected. boolean No false
disableInput If true disable the input field. boolean No false
fieldTabindex Set the tabIndex of the input. number No
initialValue Initial value for the component. Value is parsed using: titleField, descriptionField and imageField and used as selected value any No
inputId id attribute of the input element. string No
inputName name attribute of the input element. string No
inputClass class attribute of the input element. string No
matchClass CSS class to apply for matching part of the title and description. string No
maxChars Maximal number of characters that the user can type in the component. number No 524288
minSearchLength Minimal number of characters required for searching. number No 3
overrideSuggested If true will override suggested and set the model with the value in the input field. boolean No false
openOnFocus If true will open the dropdown and perform search when the input gets the focus. boolean No false
openOnClick If true will open and close the dropdown by click. boolean No false
selectOnFocus If true will select the input text upon focus. boolean No false
selectOnClick If true will select the input text by click. boolean No false
fillHighlighted If true will set the model with the value in the input field when item is highlighted. boolean No true
pause Number of msec. to wait before searching. number No 250
placeholder Placeholder text for the search field. string No
textNoResults Text displayed when the search returned no results. if the string is falsy it won't be displayed string No
textSearching Text displayed while search is active. if the string is falsy it won't be displayed string No Searching...
autoHighlight Automatically highlight the best matching search result when the input changes. the "best match" is selected by: exact match, starts with and finally includes boolean No false

ng2-completer events

Name Description Type
selected emitted when an item is selected. (selected: CompleterItem): void
highlighted emitted when an item is highlighted. (highlighted: CompleterItem): void
focus emitted when the input gets focus (): void
blur emitted when the input looses focus (): void
opened emitted when the dropdown is opened or closed (isOpen: boolean): void
keyup emitted when the input emits keyup (event: any): void
keydown emitted when the input emits keydown (event: any): void

ng2-completer methods

Method Description Parameters
open() Open the dropdown
close() Close the dropdown
focus() Set the focus to the completer input
blur() Remove the focus from the completer input
isOpen() Returns the state of the dropdown

Local data

Create local data provider by calling CompleterService.local.

Parameters

Name Type Description Required
data any[] | Observable<any[]> A JSON array with the data to use or an Observable that emits one Yes
searchFields string Comma separated list of fields to search on. Fields may contain dots for nested attributes; if empty or null all data will be returned. Yes
titleField string Name of the field to use as title for the list item. Yes

Attributes

Name Type Description
descriptionField string Name of the field to use as description for the list item.
imageField string Name of the field to use as image url for the list item.

Remote data

Create remote data provider by calling CompleterService.remote.

Parameters

Name Type Description Required
url string Base url for the search Yes
searchFields string Comma separated list of fields to search on. Fields may contain dots for nested attributes; if empty or null all data will be returned. Yes
titleField string Name of the field to use as title for the list item. Yes

Attributes

Name Type Description
descriptionField string Name of the field to use as description for the list item.
imageField string Name of the field to use as image url for the list item.
urlFormater (term: string) => string Function that get's the searchterm and returns the search url before each search.
dataField string The field in the response that includes the data.
requestOptions RequestOptions (@angular/common/http) HTTP request options that should be sent with the search request.

CSS classes

  • .completer-holder
  • .completer-input
  • .completer-dropdown-holder
  • .completer-dropdown
  • .completer-searching
  • .completer-no-results
  • .completer-row
  • .completer-image-holder
  • .completer-image
  • .completer-image-default
  • .completer-title
  • .completer-description
  • .completer-list-item-holder
  • .completer-list-item
  • .completer-selected-row

Credits

  • This product uses the TMDb API but is not endorsed or certified by TMDb

changelog

Change Log

All notable changes to this project will be documented in this file.

9.0.1(2020-04-09)

  • Upgrade to angular 9
  • Follow angular version numbers
  • Disable Ivy #448
  • Fix selected event not working #453

3.0.3 (2019-11-09)

  • Removed rxjs-compat dependency #435
  • Remove deprecated @angular/http, and move to @angular/common/http #436
  • Update to angular version 9 #439

3.0.2 (2019-06-07)

  • Angular 6 support

3.0.0-beta.2 (2018-09-10)

  • Merge PR #401 RxJS upgrade to v6, plus a few lint corrections.
  • Remove HttpClientModule from import list (fix issue #393) it should now be imported by the App module

2.0.8 (2018-06-22)

Fix issue with Observable relative link in package

2.0.7 (2018-06-22)

Release Ver 2 final

2.0.6 (2017-12-01)

Library structure and build now uses angular-library-starter

Breaking changes

  • Angular version support changed to 5.x.x
  • Use rxjs 5.5.x lettable operators

2.0.0 (2017-09-24)

Breaking changes

  • Use Angular 4 HttpClient
  • Removed RemoteData.headers method. Use RemoteData.requestOptions({headers: HttpHeaders}) instead.

1.6.3 (2017-09-01)

Fixed bugs

  • Using number on presenting text is causing an 'is not function' error #220
  • Error when ngModel initialized with a non string value #307

1.6.2 (2017-08-30)

Implemented enhancements

  • Add selectOnFocus

Fixed bugs

  • 'clearUnselected' attribute not working when using tab key #287

1.6.1 (2017-08-11)

Fixed bugs

  • Material design demo is not working #293
  • Duplicate keyup event #284
  • Fix package dependencies #288
  • Error: ViewDestroyedError #266

1.6.0 (2017-07-15)

Implemented enhancements

  • Clear selected and input value when dataSourceChange emits

Fixed bugs

  • Cannot read property 'dataSourceChange' of undefined #279
  • RemoteData search - dropdown list not showing after first http request error (e.g. no Internet connection) #131

1.5.4 (2017-07-14)

Implemented enhancements

  • Add dataSourceChange to CompleterData to detect changes in the data source

1.5.3 (2017-07-14)

Implemented enhancements

  • Add a blur() method? #217

Fixed bugs

  • Dropdown is not opened when using backspace after a value is selected #261
  • On text paste the search doesn't get triggered #265
  • "flase" should be "false" on line 202 @ ng2-completer/demo/native-cmp.html #271
  • Demo not working on Android Chrome 59 #270

1.5.2 (2017-06-16)

Fixed bugs

  • ng build --prod error: #245

1.5.1 (2017-06-14)

Fixed bugs

  • Dropdown doesn't open when typing one character #255
  • Scroll issues

1.5.0 (2017-06-09)

Implemented enhancements

  • Add strict null check to tsconfig.json #201
  • performance improvements
  • prevent flickering when not using textSearching
  • add setDisabledState to component

Fixed bugs

  • prevent esc key from bubbling up when dropdown is open
  • autofocus causing an error
  • AOT build issue with onFocus
  • searching the same term twice causes different results #216
  • minimum search length behavior weird. #232

1.4.0 (2017-04-22)

Implemented enhancements

  • Events for when dropdown is opened and closed and property to interrogate current state #146
  • openOnFocus - automatically open the dropdown and perform search when the input gets focus
  • Setting ngModel now triggers search
  • Search doesn't open the dropdown only focus or open via API will do that
  • Add option for preselected value #191
  • Support for datasource changes
  • Add autoHighlight option that will auto highlight the best matching result
  • Feature request: option to hide the drop-down if there are no results found #189
  • feature proposal: keyUp output event #186
  • how to empty search box? #114

Fixed bugs

  • Completer doesn't close when the field is cleared #194
  • Setting overrideSuggested = true gives different behavior when selecting with mouse or tab/keyboard #200

1.3.1 (2017-04-06)

Fixed bugs

  • onselect gives setImmediate is not defined #187

1.3.0 (2017-03-31)

Implemented enhancements

  • Please add focus event along with blur one #175
  • Demo redo
  • No link to Github in demo #133
  • added focus() method to completer component #152
  • Add inputId property to ng2-completer component (analogy to existing inputName property) #128

Fixed bugs

  • Relax peer dependencies to support Angular 4.0 #184
  • ng2-completer closes when scrollbar is clicked on IE browser #158

1.2.1 (2017-03-18)

Fixed bugs

  • Can't bind to 'selected' since it isn't a known property of 'ng2-completer'. #155
  • Autofocus does not work. #150

1.2.0 (2017-02-26)

Implemented enhancements

  • Replaced the source URL of remote formatter because of Google API limits
  • Replace dataService with datasource and accept Array or URL string as input for datasource. Note: dataService will be depreacated in future releases

Fixed bugs

  • Since last commit searchFields does not behave as stated in the docs #144
  • Fixed ionic build
  • Fix material design demo
  • Fix demo of remote URL formatter

1.1.0 (2017-02-10)

Implemented enhancements

  • searchFields and titleField are mandatory #100

Fixed bugs

  • (selected) not called when text is highlighted and then deleted #121
  • Clicking in a pre-filled ng2-completer with [overrideSuggested]="true", then immediately clicking another element clears the ngModel to a blank string #122
  • required not working when intializing ngModel with a value #126
  • First item is selected on enter #134

1.0.0 (2017-01-21)

Implemented enhancements

  • Input box doesn't have a type! #73
  • new ctr-input property 'fillHighlighted' that controls setting of input value when item is highlighted #123 (by @mdudek)
  • Cannot access entered text when not selected from list #72
  • ng2-completer input autofocus? #125
  • Set max-height of dropdown in demo

Fixed bugs

  • ngModelChange doesn't work #97
  • Material design dropdown pushes everything down #115

Breaking changes

  • NgModel now reflects the vlue of the input. to get only selected values use selected event

0.4.0

Implemented enhancements

  • reorder of the folder structure
  • Apply class to child input for styling purposes #40
  • Annoying flickering when clearing data #82
  • Open dropdown programmatically. #84

Fixed bugs

  • Problem with OnPush change detection strategy #69
  • AutoMatch doesn't remove binded value if nothing match (angucomplete-alt does) #101

0.3.3

Fixed bugs

  • Why version 0.3.2 requires @angular/*@2.2.4? #103
  • Build with @angular version 2.3.1 to fix issue with metadata version

0.3.2

Fixed bugs

  • Form is submitted when we select an option from dropdown using enter key #52

0.3.0

Implemented enhancements

  • Change deployment method now using ngc and rollup for the package and webpack for dev and demo
  • AOT support #60

Fixed bugs

  • TS5023 Build:Unknown compiler option 'forceConsistentCasingInFileName' #74
  • .completer-selected-row is missing in the description #78
  • originalObject is null for CompleterService in version 0.2.3 #81

0.2.3

Implemented enhancements

  • Clear selection when search changes #45

Fixed bugs:

  • fix for overrideSuggested

0.2.2

Implemented enhancements

  • Added support for async local data

Fixed bugs

  • Not able to capture blur event #50
  • textSearching not display in first search #55

0.2.1 (2016-10-05)

Implemented enhancements

  • Added material2 component to demo

Fixed bugs

  • Mouse click doesn't select the item, Enter Key does! #46

0.2.0 (2016-10-04)

This is a rewrite of the completer component using directives that implement most of the functionality.

Implemented enhancements

  • Support for custom HTML and CSS #13 #21

Fixed bugs

  • Bump version dependency to angular 2.0.0 #39