[Angular] Read Custom HTTP Headers Sent by the Server in Angular

By default the response body doesn’t contain all the data that might be needed in your app. Your server might return some special header which you have to read explicitly. In such case we can use the { observe: ‘response’} configuration of the Angular HttpClient. Let’s explore how.

import { Injectable } from ‘@angular/core‘;
import { Observable } from ‘rxjs/Observable‘;
import { HttpClient, HttpResponse } from ‘@angular/common/http‘;

export interface Person {
  name: string;
}

@Injectable()
export class PeopleService {

  constructor(private http: HttpClient) {}

  fetchPeople(): Observable<HttpResponse<Person>> {
    return this.http
      .get<Person>(‘data/people.json‘, { observe: ‘response‘});
  }
}

Now instead of just returning your data, it returns your response object.

 {
  "headers": {
    "normalizedNames": [],
    "lazyUpdate": null
  },
  "status": 200,
  "statusText": "OK",
  "url": "https://run.plnkr.co/preview/cjdn2x8fh000ffillqi8d3o4k/data/people.json",
  "ok": true,
  "type": 4,
  "body": [
    {
      "name": "xxx"
    },
    {
      "name": "xxx"
    }
  ]
}

原文地址:https://www.cnblogs.com/Answer1215/p/8448897.html

时间: 2024-10-11 23:50:01

[Angular] Read Custom HTTP Headers Sent by the Server in Angular的相关文章

《Getting MEAN with Mongo Express Angular and Node》---ch08 Adding Angular components to an Express application(向Express应用添加Angular组件)

This chapter covers(本章概要)■ Getting to know Angular(了解Angrular)■ Adding Angular to an existing page(向动态页面添加Angular)■ Filtering lists of data(过滤列表数据)■ Using an API for reading data(使用API读取数据)■ Some Angular jargon: controllers, scope, filters,directives

[Angular] Create custom validators for formControl and formGroup

Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we want to validate it: form = this.fb.group({ store: this.fb.group({ branch: ['', [Validators.required, StockValidators.checkBranch]], code: ['', Valida

[ngx-formly] Use 3rd party Form Controls with Angular Formly / Custom type

In a real form you'll most likely want to add some 3rd party form controls. For example autocomplete fields, date-time pickers etc. In this lesson we're going to see how to use ng-select and configure it s.t. it can be used within our Formly form. cu

[Angular 9] Custom CSS Variables binding

Html: <input type="range" value="0" [style.--thumb-rotate]="720 * $any(input.value)/100 + 'deg'" #input (input)="0"> CSS: input[type="range"]::-webkit-slider-thumb:active { background-position: 100%

[Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2

Just like passing in an array to *ngFor, you can pass in any value into your structural directive so that it can render templates based on those values. It's crucial to understand how the *directive syntax expands into a <template> and adds a custom

[Angular Directive] Implement Structural Directive Data Binding with Context in Angular

Just like in *ngFor, you're able to pass in data into your own structural directives. This is done by declaring the variable using a let statement then passing context into the createEmbeddedView call. We know *ngFor we did like this: *ngFor="let mes

关于angular中函数的先后执行之我见(以及angular的小bug)

在js中,函数的先后执行 (1)在angular中假设有这个场景,对表单资料进行编辑,刚好这个表单有select选项需要从后台中获取,这个时候这个表单使用angular进行开发的时候的正确打开方式应该是 先加载select选项,在加载表单的对应内容(由于http是异步的,并不是单纯的把js顺序调整一下就可以的) 这时候可以使用angular自带的$q返回promise来控制函数运行, 如果函数中没有其他的异步,简单粗暴的使用$timeout来控制 (2)input[type=hidden]使用n

svn报错:This error was generated by a custom hook script on the Subversion server.

rename一下,剪切一份,覆盖一下,提交.

Custom DNS on Ubuntu 18.04LTS server

1. Edit resolved config file nano /etc/systemd/resolved.conf 2. Replace #DNS into DNS DNS=9.9.9.9 1.1.1.1 3. Restart systemd service service systemd-resolved restart 4. Finally, check the status systemd-resolve --status 原文地址:https://www.cnblogs.com/s