Retrieving Child Route Parameters in Angular 7

Fetching the parameter 'id' only from the corresponding page component seems to be a challenge. The params id cannot be accessed from other individual components such as the header component.

//The code snippet below works only in the corresponding component

 this.activatedRoute.paramMap.subscribe( params => {
      this.projectName  = params.get("id");
      console.log(this.projectName);
    })

//I attempted these codes, but faced an issue
    console.log(this.activatedRoute.children[0].children[1].params['id'])
    this.activatedRoute.firstChild.params.subscribe(params => {
      let id = +params['id'];
      console.log(id)
    });
 A console error occurred (line number indicates where I tried to log on the page) at HeaderComponent.push../src/app/common/header/header.component.ts.HeaderComponent.ngOnInit

Structure

<app-header></app-header>
<router-outlet></router-outlet>

app.module.ts
app-routing.module.ts
common/
--header.component.ts
projects/
  component-folder
--projects.module.ts
--projects-routing.module.ts (child route)

Alternatively, is there a way to access the params from directives?

Answer №1

If you are looking for a way to retrieve the value of param id from other specific components (such as headerComponent), I recommend utilizing the Router NavigationEnd event. This approach will likely provide the desired result.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining additional information for Observable<Object[]>

I have a scenario where I am working with a service that returns Observable<Object[]>. Each Object in the array has a subObjectId property. My goal is to populate the object's subObject property with data retrieved from another service. How can ...

Unable to convert the value "Firefox" to the specified type 'Microsoft.VisualStudio.WebClient.Diagnostics.HtmlToolHost.PineZorro.DebugAdapterType'

I'm looking to switch from using Chrome to Firefox for my Angular project. I successfully installed the debug adapter from this link and it's working properly. However, when I attempted to replace launch.json in Vs2022, I encountered the followi ...

What is the best way to establish communication between the browser and an express.js server while utilizing angular ssr?

I've encountered a server-side rendering (SSR) issue that does not seem to be addressed in either the Angular documentation or the new Angular developer documentation. My inquiry pertains to transferring data from the browser to the server, as oppose ...

Invoke the custom form validator multiple times

My approach involves using a specialized validator to ensure that the values of two input fields are in sync: import { FormGroup } from '@angular/forms'; // custom validator to check that two fields match export function MustMatch(controlName: ...

The Angular 11 library module has been successfully imported into the consuming app but it is not being utilized

Currently, I am in the process of creating an Angular library that will encompass services, pipes, and directives to be utilized across various Angular projects within my organization. At this point, I have successfully implemented three services within th ...

Implementing a back button in an RTL layout with Ionic 2

Just starting an Ionic 2 app in Arabic language requires a RTL layout. I decided to go with the side menu template. Adding the following line for configuring the app to RTL perfectly changed everything's direction, except for the back button which st ...

Developing an angular progress bar

I've been working on creating a progress bar in Angular using the mmat-stepper. Here's a snippet of my code: import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppCom ...

Angular HttpClient request fails to initiate

Overview: A button click on a form triggers the methodForm within the component. methodForm then calls methodService in the service layer. methodService is supposed to make an HTTP POST request. Problem: The HTTP POST request is not being made. However, me ...

RxJS: the art of triggering and handling errors

This is more of a syntax question rather than a bug I'm facing. The process is straightforward: Send an HTTP request that returns a boolean value If the boolean is true, proceed If the boolean is false, log a warning and stop the flow. To handle ...

Error in Angular 11: Unspecified parameter in the URL

I've been working on passing a URL parameter received from a GET request to the Contracts component. However, when visiting the Contracts component in the URL, the passed parameter appears as undefined. Here's what I have tried so far: In my app ...

Angular 2 Popup Modal Issue: "Expression modified after checking"

See the problem in action on YouTube Check out the GitHub repository for the demo app My simple app consists of an app component, a child component (account), and an alert service that handles a message dialog component (popup modal). To demonstrate the ...

utilize Java version specifically designed for integrating with Angular framework and SOAP web services

One question I've been pondering: we currently have a project built on Java 6 and GWT, but we're considering migrating to Angular (using SOAP web services). Will Java 6 be compatible with Angular for the backend, or will we need to upgrade to Jav ...

There seems to be an issue with the Angular QuickStart project as it is not functioning properly, showing the error message "(

After following the instructions in this guide for setting up VS2015, I encountered issues when trying to run the "quick start" project or the "tour of heroes" tutorial on Google Chrome. The error message I received can be found here: Angular_QuickStart_Er ...

Ways to obtain the scrollWidth and offSetWidth of a div that is being utilized as an ngx-datatable-cell-template within an ngx-datatable

How can I retrieve the scrollWidth and offSetWidth of a div that is included in a child component being loaded as an ngx-datatable-cell-template inside ngx-datatable? I am consistently receiving values of 0. I have added a template variable for the div el ...

The default behavior of Angular-Keycloak does not include automatically attaching the bearer token to my http requests

I'm currently working on integrating keycloak-angular into my project, but I'm facing an issue with setting the bearer token as the default for my HTTP requests. "keycloak-angular": "9.1.0" "keycloak-js": "16.0 ...

Developing hybrid applications without using Angular or React

As a newcomer to Hybrid App Development, I recently created a website using core coding without frameworks like Angular or React. Now, my goal is to transform this website into a Hybrid App. However, I'm facing some lingering questions that I couldn& ...

Hovering over the Chart.js tooltip does not display the labels as expected

How can I show the numberValue value as a label on hover over the bar chart? Despite trying various methods, nothing seems to appear when hovering over the bars. Below is the code snippet: getBarChart() { this.http.get(API).subscribe({ next: (d ...

Icon positioned to the left within the text box

Hey there! I'm new to NativeScript and I've been struggling to place an icon inside a textbox. Can someone please help me out? Expected Output: https://i.stack.imgur.com/xvoZG.png Code <GridLayout columns="*, *" rows=& ...

Guide on implementing a .catch method in Firebase's onSnapshot function

I have recently developed an Ionic Firebase chat application. I seem to be encountering an issue with setting up a query snapshot when initializing the message page. Here is the code snippet that I am using: ngOnInit() { this.messageService.getA ...

Oops! An error occurred: Uncaught promise in TypeError - Unable to map property 'map' as it is undefined

Encountering an error specifically when attempting to return a value from the catch block. Wondering if there is a mistake in the approach. Why is it not possible to return an observable from catch? .ts getMyTopic() { return this.topicSer.getMyTopi ...