0

I'm trying to apply the autocomplete feature in my angular project, but it does not works.

Here code -

<!-- Start ignoring BootLintBear -->
<form class="navbar-form navbar-left">
<!-- Stop ignoring BootLintBear -->
  <div class="input-group" id="nav-group">
    <input #input type="text" name="query" class="form-control" id="nav-input" (keyup)="onquery($event)"
           [(ngModel)]="searchdata.query">
           <div id="output"></div>
           <script>
             function suggestMe(data) {
               document.getElementById('output').innerHTML = data;
             }
             var storeData = '';
             document.getElementById('nav-input').onkeyup = function() {
               if(storeData!== ''){
                  document.body.removeChild(storeData);
               }
             var textBox = document.getElementById('nav-input').value;
             storeData = document.createElement('script');
             storeData.src = 'http://en.wikipedia.org/w/api.php?action=opensearch&limit=10&format=json&callback=suggestMe&search=' + textBox;
             document.body.appendChild(storeData);
             }; 
            </script>
    <div class="input-group-btn">
      <button class="btn btn-default" id="nav-button" type="submit" (click)="submit()">
        <i class="glyphicon glyphicon-search"></i>
      </button>
    </div>
  </div>
</form>

Suggestions are welcomed : )

Harshit
  • 1,510
  • 19
  • 42
  • why are you using plain javascript inside your HTML. you can go with available directives – Aravind Mar 24 '17 at 22:13
  • you can use jQuery autocomplete. – Roman C Mar 24 '17 at 22:17
  • @Aravind Can you suggest me the directives? It will be helpful for me, as I'm trying to apply autocomplete feature. : ) – Harshit Mar 24 '17 at 22:17
  • 1
    @harshit98 Have a look at this [**answer**](http://stackoverflow.com/questions/43007815/ngbtypeahead-selectitem-get-clicked-item-ngbootstrap-angular2/43008142#43008142) – Aravind Mar 24 '17 at 22:27

1 Answers1

4

Instead of mixing pure JavaScript with Typescript, why not do it in full Typescript ? Try this way or adapt it to your need. First set up an observable stream

  data: Observable<any>;
  private searchTerms = new Subject<string>();

  onquery(term: string): void {
    this.searchTerms.next(term);
  }

Next, set up the search feature

  ngOnInit(): void {
    this.data = this.searchTerms
      .debounceTime(300)        // pause in events
      .distinctUntilChanged()   // ignore if search term not changed
      .switchMap(term => term   // switch to new observable each time
        //http service to retrieve your data
        ? this.searchService.search(term)
        : Observable.of<any>([])
      )
      .catch(error => {
        console.log(error);
      });

  }

Template :

<input #input type="text" name="query" class="form-control" id="nav-input" (keyup)="onquery(input.value)">

<div id="output">
   <div *ngFor="let item of (data | async)">{{ item }}</div>
</div>
mickdev
  • 2,765
  • 11
  • 15
  • Hey! Thanks! I liked this idea very much. I'm actually, new to TypeScript. Can you review my code here, it will be very much helpful for me - https://jsfiddle.net/harshit98/ueu70rar/ : ) I have done some changes according to your suggestions. – Harshit Mar 25 '17 at 09:03
  • I'll be happy to help. You should move your code in a [Plunker](https://plnkr.co/) and I'll take a look – mickdev Mar 25 '17 at 12:05
  • done, here- https://plnkr.co/edit/gBz7NytELWc5dYCNwULa?p=preview . Take a look at `index.html` and `search-bar.component.ts` – Harshit Mar 25 '17 at 12:12
  • I was debugging your code and I saw **Aravind** Plunker. It's exactly what you want did you take a look to it ? http://plnkr.co/edit/BX6Vv3k4ei0O3m2F5Kpn?p=preview – mickdev Mar 25 '17 at 14:44
  • Yes, this is what I exactly want : ) – Harshit Mar 26 '17 at 09:24
  • I created a `autocomplete.service.ts` and I'll import this module in `app.module.ts`. Please review the code here - https://plnkr.co/edit/gBz7NytELWc5dYCNwULa?p=preview . I don't know when I use `switchMap` or `request` why it says "Cannot find switchMap" or "Cannot find request" ? It will be very helpful if you can correct my mistakes in the code. – Harshit Mar 26 '17 at 09:36
  • change this line `.map((request)) => request.json()[1];` to `.map(response => response.json() )`. You can let request if you want but technically it's a response. Your Plunker is not functional so even if I modify it with the right code it won't work – mickdev Mar 27 '17 at 01:15
  • Thanks for the help, really appreciated :) – Harshit Mar 27 '17 at 04:52