输入Observable <observable <any []>&gt;不能分配给Observable <any []>类型

时间:2018-03-06 12:32:55

标签: angular typescript rxjs rxjs5

我正在使用来自数据库服务的数据实现自动完成:

@Injectable()
export class SchoolService {
  constructor(private db: AngularFirestore) {
  }

  getSchools(): Observable<School[]> {
    return this.db.collection<School>('schools').valueChanges();
  }
}

在我的组件中:

export class SchoolComponent implements OnInit {
  formControl: FormControl = new FormControl();
  schools: Observable<School[]>;
  filteredSchools: Observable<School[]>;

  constructor(private schoolService: SchoolService) {
  }

  ngOnInit() {
    this.schools = this.schoolService.getSchools();

    //Below line gives error "Type Observable<Observable<School[]>> is not assignable to type Observable<School[]>".
    this.filteredSchools = this.formControl.valueChanges.pipe(
      startWith(''),
      map(name => this.filterSchools(name))
    );
  }

  filterSchools(name: string): Observable<School[]> {
    return this.schools.map(school => school.filter(s => s.name.toLowerCase().includes(name)));
  }
}

我的HTML:

<form>
  <mat-form-field>
    <input type="text" matInput placeholder="Type Your School to Continue" [matAutocomplete]="auto"
           [formControl]="formControl">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let school of filteredSchools | async" [value]="school">
        <span>{{school.name}}</span> |
        <small>{{school.city}}</small>
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

ngOnInit中的功能会导致Type Observable<Observable<School[]>> is not assignable to type Observable<School[]>错误。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

错误说明究竟出了什么问题。你定义了:

filteredSchools: Observable<School[]>;

但稍后你创建了一个Observable,它发出(映射每个值)filterSchools(name: string): Observable<School[]>的结果,它返回一个Observable,所以最后你有一个Observable链Observable<Observable<School[]>>

实际上,您似乎只想使用switchMap(或mergeMapconcatMap)代替mapswitchMap将订阅嵌套的Observable并将链转换为Observable<School[]>

this.filteredSchools = this.formControl.valueChanges.pipe(
  startWith(''),
  switchMap(name => this.filterSchools(name))
);

答案 1 :(得分:0)

我建议使用Observable.combineLatest(...)

public ngOnInit(): void {
   this.schools = this.schoolService.getSchools();

   this.filteredSchools = Observable.combineLatest(
      this.formControl.valueChanges.startWith(''),
      this.schools
   ).map(([filter, schools]) => {
       if (!filter || filter === '') {
          return schools;
       }

       return schools.filter(s => s.name.toLowerCase().includes(name));
   });
}

最后仍有冷观察,只有当其中一个观察者发出新数据时才会触发。

不是您的问题,但对于您的模板自动填充,您需要displayWith功能。

相关问题