首页 > 解决方案 > 使用 observable 按字母顺序排序数据

问题描述

我正在使用 angular 8 和 asp.net 核心,所以我想在下拉列表中显示客户的姓名作为 api 服务的字母顺序,我找到了使用 observable 的唯一解决方案。还有其他方法可以实现吗?以及如何在 observable 中使用字母排序?

getAllcustomer() {
    return this._http.get<any>(this.myAppUrl + '/api/users/getcustomer', {responseType: 'json'} )
      .catch(this.errorHandler);
}

谢谢

标签: c#angularasp.net-coreobservable

解决方案


您可以映射响应:

getAllcustomer() {
    return this._http.get<any>(this.myAppUrl + '/api/users/getcustomer', {responseType: 'json'} ).pipe(
  // assuming this API call gives you all the customers as an array of strings.
  map((response: any) => response.sort()), 
)
      .catch(this.errorHandler);
}

将此排序放入服务中是可选的,您也可以在组件本身中进行排序。


推荐阅读