5

I'm a student starting to train on Angular 5. I guess my issue could be the same even on Angular 2.

For a project, i'm currently trying to create a simple search input where the user can write a name of a city, and the field will suggest some names below. Then when the user click on a item of the list, it will be displayed in the input. Actually it should act pretty much like the google search bar.

Here is the plunk. I guess it won't run in plunk because i just put the right files to understand and not my whole app.

Here are my files : input-form.component.html :

<input
     type="text"
     class="form-control"
     id="InputCity"
     [(ngModel)]="searchText"
     formControlName="city"
     (click)="displayCities()">
        <ul class="results">
            <li>
               <a 
                 class="list-group-item"
                 style="cursor: pointer"
                 *ngFor="let city of citiesListInput | filter : searchText"
                 (click)="putIntoField(city)">
                   {{ city }}
                </a>
            </li>
        </ul>

input-form.component.ts (in short):

export class InputFormComponent implements OnInit {
  citiesListInput: string[];

  constructor(private citiesSvc: CitiesService) { }

  //Get list of cities from a service//
  displayCities() {
    this.citiesListInput = this.citiesSvc.citiesList;
  }

  //Should send the city clicked in the list in the input field.// 
  //For now i just console log it//
  putIntoField(city: string) {
    console.log(city);
  }
}

First issue : The second click event (putintoField) does not seems to work in a list of items. It just does nothing. But if i remove ul and li tags it works. Am i missing something ? If there a way to do it with ul and li tags ?

Second issue : What should i do to put the selected city in the input field ? I'm just missing how to select the value of my input and replace it.

It seems rather simple but i cannot find the right answer on the internets.

Thanks everyone for your answers !

And this is my filter file which is declare in app module :

import { Pipe, PipeTransform } from '@angular/core';

  @Pipe({
    name: 'filter'
  })

export class FilterPipe implements PipeTransform {
  transform(items: any[], searchText: string): any[] {
    if(!items) return [];
    if(!searchText) return items;

    searchText = searchText.toUpperCase();

    return items.filter( it => {
      return it.toUpperCase().includes(searchText);
    });
  }
}
Linpter
  • 201
  • 3
  • 17
  • I would say to just use material. They have a component for this and it would save you from reinventing the wheel. https://material.angular.io/components/autocomplete/overview – mgm87 Jan 10 '18 at 14:51
  • Your question isn't clear. Where you got `filter` pipe and I'm not understand `this.citiesListInput = this.citiesSvc.citiesList;` is it promise or observable request ? – Imran Jan 10 '18 at 14:53
  • @mgm87 please don't suggest third party plugin. focus this question or not – Imran Jan 10 '18 at 14:55
  • @Imran Hi, I didn't precise the service and the filter files because they are not involved in the issue. The first just permits to get a array of city names and the second allows to filter the array when the user is typing a name. If you remove them the issue still remains. Do you think it could be linked ? What is not clear in my initial question ? – Linpter Jan 10 '18 at 15:12
  • @mgm87 Indeed, this autocomplete is what i'm trying to do. In case i can't make my code works i'll maybe try it. Thank you. But i really would like to understand what is not working in my code, since it looks rather simple..I feel that if i don't understand that, i can't be good at coding in Angular... – Linpter Jan 10 '18 at 15:21
  • tell me about your filter – Imran Jan 10 '18 at 15:33
  • ok can you show me that filter pipe code ? – Imran Jan 10 '18 at 15:43
  • I added to my post – Linpter Jan 10 '18 at 15:45

1 Answers1

1

First issue I don't get any problem in your code. may be you can bind your ul with *ngif (you can replace your citiesListInput: string[]; to citiesListInput = [] declaration . and check like *ngIf="citiesListInput.length". Note: I was replace your array this.citiesListInput = ['dhaka', 'sylet', 'manikganj']; instead of this.citiesListInput = this.citiesSvc.citiesList; and its work fine

Second issue just assign your model

putIntoField(city: string) {
  this.searchText = city;
  console.log(city);
}
Imran
  • 3,031
  • 4
  • 25
  • 41
  • Thank you very much ! i'll try that. I'm still concerned about why my initial code doesn't work. It means that something else is not working...Anyway i'll see that after the changes. – Linpter Jan 11 '18 at 08:16
  • 1st issue : Replacing citiesListInput: string[]; to citiesListInput = [] made my compile failed. 2nd issue : it doesnt recognize searchText. This is the error : Property 'searchText' does not exist on type 'InputFormComponent'. I think i'm missing some basics things. – Linpter Jan 11 '18 at 08:35
  • for your second issue declare a property `public searchText: string` – Imran Jan 11 '18 at 08:50
  • what error you get for your first issue. please show me the error code – Imran Jan 11 '18 at 08:51
  • A not very friendly code error : ERROR in Error: Error: Debug Failure. at typeToString (*path of the file*) at reportRelationError at isRelatedTo at checkTypeRelatedTo at checkTypeAssignableTo at checkAssignmentOperator at checkBinaryLikeExpression at checkBinaryExpression at checkExpressionWorker at checkExpression at checkExpressionStatement at checkSourceElement at Object.forEach at checkBlock at checkSourceElement at checkFunctionOrMethodDeclaration – Linpter Jan 11 '18 at 09:20
  • i think it's `citiesListInput` property declaration problem. please recheck and assign some array element in your `displayCities` function like `this.citiesListInput = ['dhaka', 'sylet', 'manikganj']` – Imran Jan 11 '18 at 09:25
  • 1
    I guess it could be linked to the fact that i'm using a service for citiesListInput – Linpter Jan 11 '18 at 09:27
  • Ok i'll do that soon i can – Linpter Jan 11 '18 at 09:44
  • I edit my post with the plunk link. Thanks again Imran – Linpter Jan 11 '18 at 12:32