0

I was trying to find answer but was unable to, so my question is simple, i have this function

  getProducts(projectId, pageSize, page) {
    return new Promise((resolve, reject) => {
      this.http.get('/api/projects/' + projectId + '/products/?pageSize=' + pageSize + '&page=' + page).subscribe(data => {
        resolve(data);
      }, error => {
        reject(error)
      });
    });
  }

I'm trying to read pagination custom header, and in browser response i can see it, but i'm not sure how to access it with my function, on old angular i was using interceptors but on angular 2+ i'm unable to make it work, anyone can show me how to do it ?

5
  • is that Http or HttpClient? Commented Sep 1, 2017 at 9:39
  • what is version of angualr your are using 4 + Commented Sep 1, 2017 at 9:39
  • @Jota.Toledo haha, i guess we dnt have work (me and you ) Commented Sep 1, 2017 at 9:39
  • Im using HttpClient, and this is latest version of angular its 4+ Commented Sep 1, 2017 at 9:40
  • 1
    make use of http interceptors as you had done in angular js check this link for interceptors link Commented Sep 1, 2017 at 9:50

1 Answer 1

2

So after i investigated many possibilities i found best and most simple solution, i hope this helps someone. I use HttpClient and angular 4+, just simply add new parameter in http.get methd {observe: 'response'}

  getProjects(projectId, pageSize, page) {
    return new Promise((resolve, reject) => {
      this.http.get('/api/projects/' + projectId + '/products/?pageSize=' + pageSize + '&page=' + page,
        { observe: 'response' }).subscribe(data => {
          const pagination = JSON.parse(data.headers.get('pagination'));
          resolve({
            items: data.body,
            totalItems: pagination.TotalItems
          });
        }, error => {
          reject(error)
        });
    });
  } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.