0.5.17 • Published 8 years ago

ng2-typeahead-se v0.5.17

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

ng2-typeahead npm version

A simple Angular2 typeahead/autocomplete component with no third-party dependencies.

See ng2-typeahead on GitHub

Angular 2 Style Guide Join the chat at https://gitter.im/brinkmanjg/ng2-bootstrap Dependency Status devDependency Status Throughput Graph

Installation

  1. A recommended way to install ng2-typeahead is through the npm package manager using the following command:

    npm i ng2-typeahead --save

    Alternatively, you can download it in a ZIP file.

  2. Currently ng2-typeahead contains one directive: typeahead.

Example

my.component.ts
import {Typeahead} from 'ng2-typeahead'

@Component({
    selector: 'my-component',
    template: require('./my.component.html'),
    directives: [Typeahead]
})
export class MyComponent {

  fruits: any[] = [
    {
      id: 1,
      name: "1 - Apple",
      searchText: "apple"
    },
    {
      id: 2,
      name: "2 - Orange",
      searchText: "orange"
    },
    {
      id: 3,
      name: "3 - Banana",
      searchText: "banana"
    }
  ];

  selectedFruit: any;

  public fruitSelected(fruit) {
    this.selectedFruit = fruit;
  }

}
my.component.html
<typeahead
  [list]="items"
  [searchProperty]="'searchText'" [displayProperty]="'name'"
  [maxSuggestions]="2"
  (suggestionSelected)="fruitSelected($event)"
  placeholder="Begin typing a fruit">
</typeahead>
Demo

npm.io

API for typeahead

This is the only directive. Provide a list of suggestions as an object array, specify the display and search properties, and handle the selection event however you like.

Properties

Binding PropertyTypeRemarks
[list]any[]The complete list of items. These can be any type of object. This is required.
[displayProperty]stringThe property of a list item that should be displayed. The default is 'name'.
[searchProperty]stringThe property of a list item that should be used for matching. The default is 'name'.
[maxSuggestions]numberThe maximum number of suggestions to display. The default is -1 (no limit).

Note: displayProperty and searchProperty can be the same property or different properties based on your needs.

Events

Event BindingRemarks
(suggestionSelected)Called when a suggestion has been selected. The only parameter is the selected item.

Styles

SelectorRemarks
.typeaheadThe outer div which holds all component elements.
.typeahead-inputThe input element into which the user enters text.
.typeahead-input-has-selectionThe input element into which the user enters text when a suggestion is selected. This alerts the user that a selection has been made.
.typeahead-typeaheadThe type-ahead input element which displays the suggested text.
.typeahead-suggestionsThe div which holds the suggestions elements.
.typeahead-suggestions ulThe unordered list of suggestions.
.typeahead-suggestions ul liThe individual suggestion elements.
.typeahead-suggestion-activeThe active (highlighted) suggestion in the suggestions list.

Troubleshooting

Please follow this guidelines when reporting bugs and feature requests:

  1. Use GitHub Issues board to report bugs and feature requests (not our email address)
  2. Please always write steps to reproduce the error. That way we can focus on fixing the bug, not scratching our heads trying to reproduce it.

Thanks for understanding, and apologies for any issues experienced thus far.

License

The MIT License (see the LICENSE file for the full text)