【发布时间】:2019-07-04 09:45:10
【问题描述】:
我正在从服务中的 api 获取我的数据。现在我必须按名称进行输入搜索过滤。
这是我的代码服务:
export class ListService {
listUrl = 'https://swapi.co/api/planets';
constructor(private http: HttpClient) {}
getList(): Observable<Dummy> {
return this.http.get<Dummy>(this.listUrl);
}
}
主要组件:
export class MainComponent implements OnInit {
public planetList: Planet[] = [];
constructor(private listService: ListService) {}
ngOnInit() {
this.listService.getList().subscribe(list => {
this.planetList = list.results;
});
}
onSearchValueChanges(inputElement: HTMLInputElement) {}
主组件 HTML:
<input
#searchInput
type="text"
(keyup)="onSearchValueChanges(searchInput)"
placeholder="Search"
/>
<app-list [planetsList]="planetList"></app-list>
列表组件:
export class ListComponent implements OnChanges{
@Input() planetsList: Planet[] = [];
ngOnChanges() {
console.log(this.planetsList)
}
}
在主组件中过滤 this.planetlist 数组是最佳选择吗?或者也许我应该使用 RxJS 过滤器?如果有怎么办?
提前感谢您的回答!
【问题讨论】: