0

I have a form with mat-autocomplete and mat-chip-list components. It works fine with static data that i have in an array. But when i try to load data remotly from a service i have an error :

this.carCtrl.valueChanges.startWith is not a function

Here's my code in the Controller :

import { startWith } from 'rxjs/operators/startWith';
import { map} from 'rxjs/operators/map';

cars: Car[] = [{ id: 1, brand: "honda"}, { id: 2, brand: "toyota"}];

constructor(public dialogRef: MatDialogRef<DialogForm>, @Inject(MAT_DIALOG_DATA) public data: any, public formBuilder: FormBuilder, private carService: CarService) {

 // It gives me the error mentionned above
this.carCtrl.valueChanges
.startWith(null)
.subscribe(val => {
  if (val && typeof val !== 'object') {
    this.carService.getCars(this.search).subscribe(cars=> {
      this.filteredCars = cars;
    });
  }
});

// this Works with a static array
this.filteredCars = this.carCtrl.valueChanges.pipe(
  startWith(null),
  map((car: Car | null) => this.cars.slice()));

And my template :

<input placeholder="Add cars..."
       #carInput
       [formControl]="carCtrl"
       [matAutocomplete]="autoCars"
       [matChipInputFor]="chipEbomList"
       [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
       [matChipInputAddOnBlur]="addOnBlur"
       (matChipInputTokenEnd)="add($event,'car')">
lory
  • 635
  • 2
  • 10
  • 20
  • You should add (or check yourself) a dump of data in array and data you fetch from service. – juanmf Jul 05 '18 at 17:08
  • Like i wrote in my post, data in an array works, but i'm looking to make it work from a service – lory Jul 05 '18 at 17:39

0 Answers0