[Angular 2] Using Promise to Http
2023-09-14 08:59:20 时间
You can also use Promise for http:
So for the service, you need to call toPromise() method:
getVehicles(value?: string) { return this._http.get('api/vehicles.json') .map((response: Response) => <Vehicle[]>response.json().data) .toPromise() .catch(this.handleError); }
Then in your controller, you can get the Promise back:
getHeroes(value?: string) { this.vehicles = this._vehicleService.getVehicles(value); }
But notice that, we assign the Promise to the this.vehices. so it means we use 'async' pipe:
<ul> <li *ngFor="#vehicle of vehicles | async" (click)="select(vehicle)"> {{ vehicle.name }} </li> </ul>
相关文章
- [Angular 8] Lazy loading with dynamic loading syntax
- [Angular] ngClass conditional
- [Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface
- [Angular] Protect The Session Id with https and http only
- [Angular2 Animation] Use Keyframes for Fine-Tuned Angular 2 Animations
- [Angular2 Animation] Control Undefined Angular 2 States with void State
- [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>
- [Angular 2] BYPASSING PROVIDERS IN ANGULAR 2
- [Angular 2] Controlling Rx Subscriptions with Async Pipe and BehaviorSubjects
- [Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer
- [ngx-formly] Dynamically set Model Properties with Angular Formly Expressions
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
- [Angular] Progress HTTP Events with 'HttpRequest'
- [Angular-Scaled Web] 8. Using $http to load JSON data
- 使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
- Angular groupBy test
- Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结
- Angular 应用里几种 Component 的分类阐述
- 使用 RxJS timeout 操作符给 Angular SSR 服务器端渲染模式下的 HTTP 请求添加超时机制
- Angular reducer第一次被框架调用的单步调试细节
- 【K8S】Docker向私有仓库拉取/推送镜像报错(http: server gave HTTP response to HTTPS client)