0

This is my app.modules

const appRoutes: Routes = [
  { path: 'topic/:topicTypeAngular', component: StoryTypeComponent, pathMatch : 'full'},
  { path: '**', component: PageNotFoundComponent}
];

This is my nav in html

<ul>
    <li> <a routerLink="topic/culture">Culture</a></li>
    <li><a routerLink="topic/tech">Tech</a></li>
</ul>

// Thid is component.ts excerpt and crucial part

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpRequest, HttpEvent, HttpEventType, HttpParams } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';

   constructor(public http: HttpClient, private route: ActivatedRoute) {
         this.parameter = this.route.snapshot.paramMap.get('topicTypeAngular');
         let parameter = this.parameter;
    }


    ngOnInit() {
         this.route.url.subscribe(parameter => {
         let parameter = this.parameter;
          this.http.get('http://localhost:3200/cookbooks/topic/'+parameter).subscribe(httpResponse => {
              this.data = httpResponse;
              console.log(httpResponse);
            });

        })
     }

The last part does not provide me new httpResponse data. This link similar to my issue but not exact. However, I tried but failed. Thanks in advance

2
  • Why won't you subscribe to params instead? And why not use the ngOnInit(), It is provided to doing these things Commented Dec 9, 2018 at 12:37
  • I did but does not work Commented Dec 9, 2018 at 12:40

2 Answers 2

2

What about directly getting the params with a pipe like this :

constructor(public http: HttpClient, private route: ActivatedRoute) {    
  this.route.paramMap.pipe(
    switchMap((params: ParamMap) => {
      this.parameter = params.get('topicTypeAngular');
      return this.http.get(`http://localhost:3200/something/topic/${this.parameter}`);
    })
  ).subscribe(
    result => console.log(result)
  );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly. Also, placing this on a lifecycle method and using the async pipe instead of subscribing would prefect this answer.
1

Try this:

constructor(public http: HttpClient, private route: ActivatedRoute) {}

    ngOnInit() {

    this.route.params.subscribe(param => this.http.get('http://localhost:3200/something/topic/' + param['topicTypeAngular']).subscribe(httpResponse => {
            this.data = httpResponse;
            console.log(httpResponse);
        });)
 }

2 Comments

perfect. you just missed a parenthesis ')'
Type it from my phone ☺

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.