mercredi 8 février 2017

How to cast JSON response to array in TypeAhead Angular 2

Vote count: 0

I'm trying to cast a response to an array from a JSON service in Angular 2. The code so far is as follows for the JSON service:

export class QueryService {

  constructor(private jsonp: Jsonp, private http: Http) {}

  private options = new RequestOptions({headers: new Headers({'Content-Type': 'application/json'})});

  getQuery(term: string) {

    let params = new URLSearchParams()

    console.log(term);

    params.set("query", term);

    this.options.search = params;

    console.log(params.toString());

    return this.http
                .get('http://localhost:61227/machine/', this.options)
                .map((response:Response) => {
                    console.log(response.json());
                    return response.json();
                });
  }
}

The typeahead code uses a static array at the moment like this:

export class AutoComplete {

  query: string = '';
  filteredList: any[] = [];
  elementRef: ElementRef;

  constructor(private el: ElementRef, private queryService: QueryService) {
    this.elementRef = el;
  }

  pos: number = -1;
  opened: boolean = false;
  selectedItem: any;
  item: any;
  items: any[] = [
     { id: 1, name: 'Darth Vader' },
     { id: 2, name: 'Kylo Ren' },
     { id: 3, name: 'Rey' }
   ];

I'm struggling with using the service to filter the search query?

asked 19 secs ago

Let's block ads! (Why?)



How to cast JSON response to array in TypeAhead Angular 2

Aucun commentaire:

Enregistrer un commentaire