Changing routes in Angular causes the @input value to update

Working with Angular 8, I have a setup with two components: the parent component and the child component.

The parent component needs to pass an object or string when the URL parameters change.

In the parent component, I implemented this code snippet that detects changes in the URL and sets the urlParam for the child component:

  this.activatedRoute.queryParamMap.subscribe((paramMap: ParamMap) => {
      this.urlParam = JSON.stringify(paramMap);
   
  });

Parent HTML:

<search [searchCriteriaForm]="searchCriteriaForm" [urlParam]="urlParam"></search>

In the child component, I added the following:

 @Input() set urlParam(urlParam: string) {
  console.log('The child has successfully received it');
 }

Although the data reaches the parent component when the URL changes, unfortunately, the child component does not receive it.

I noticed that upon initially loading the page, the child component receives the parameters perfectly. However, if I navigate to a different URL without reloading or refreshing the browser page, only the parent component is able to access the updated data.

Answer №1

If you're struggling to identify the issue, my recommendation would be to take a reactive approach in this case.

import { BehaviorSubject } from 'rxjs';

// Define this at the field level
urlParam = new BehaviorSubject("");
//...

  this.activatedRoute.queryParamMap.subscribe((paramMap: ParamMap) => {
      this.urlParam.next(JSON.stringify(paramMap));
  });

html

<search [searchCriteriaForm]="searchCriteriaForm" [urlParam]="urlParam | async"></search>

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

Learn how to send or submit data using the Form.io form builder

I am currently working on a dynamic drag and drop form builder, and I'm struggling to figure out how to log the data from the form. Below is the component.html file where I am implementing the drag and drop form: <div> <form-builder ...

Use RxJS to repeatedly make API calls until data for all items is retrieved

My task is to make an API call and retrieve all items from a specific endpoint, but we are limited to fetching only 100 items at a time. This is how the response object is structured: { elements: Array<any>, totalCount: number, } The endpo ...

Error occurred when attempting to link user services with report controller

Greetings, I am currently working on a Nest Js mini project that consists of two separate modules: reports and users. Each module has its own Service, controller, and repositories. The reports module has a many-to-one relation with the users module. My g ...

Extract the value from an array of objects

https://i.sstatic.net/fTShc.png Having some difficulty accessing the elements of an array. In order to assign a project name to a local variable projectName, I am seeking assistance with extracting the project name from the given JSON structure. Any hel ...

To effectively manage the form, I must carefully monitor any modifications and update the SAVE button accordingly in an Angular application

Are you experiencing an issue with detecting any changes on a page, where there is some information displayed and even if no changes are present, the SAVE button remains active for clicking? ngOnInit(): void { this.createConfigForm() th ...

Change to a different text field without the need to press the tab key by utilizing

I have set up three different input boxes and I want to know how to switch directly to the next text box without having to press the tab key on the keyboard. The values are stored in a variable called newStringPin. <input type="text" style="width: 38px ...

Persistent Ionic tabs after user logs out - keeping tabs active post-logout

My Ionic app features tabs and authentication. While the authentication process works perfectly, I am facing an issue with the tabs still displaying after logging out. Below is my login method: this.authProvider.loginUser(email, password).then( auth ...

Error: The binding element titled implicitly possesses a type of 'any'

Encountering the following issue: ERROR in src/components/Header.tsx:6:18 TS7031: Binding element 'title' implicitly has an 'any' type. 4 | 5 | 6 | const Header = ({title}) => { | ^^^^^ 7 | return( 8 | ...

Angular automatically interprets strings as dates

Currently, I have numerous entities that have been defined: export class Meeting implements IHasId { id = 0; locationId = 0; meetTime = new Date(); isFinalized = false; imageId: number = null; description = ''; name = ...

Detecting the presence of a session in Angular2 and nodejs using express

My server-side session creation is functioning properly, but I need to be able to hide certain elements in an Angular2 component template depending on whether the session exists. How can I check within my Angular2 component whether the server-created sessi ...

Peer dependency conflict detected (while executing ng update @angular/core) - explanation provided

Currently, I am in the process of upgrading my Angular project to version 11.0.5 by executing the command provided below: ng update @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="066569746346373728362833">[email ...

Set Ngx admin background image as your design foundation

Looking to add a background image to ngx-admin Check out this link for more information My goal is to achieve a similar look to this example. Sincerely, Gopi ...

Ensuring accurate date formatting of API responses in TypeScript

My REST API returns data in the format shown below:- {"id": 1, "name": "New event", "date": "2020-11-14T18:02:00"} In my React frontend app, I have an interface like this:- export interface MyEvent { id ...

Guide to customizing the layout preview specifically for certain components in Storybook, without affecting all components

Currently working on my storybook project and facing an issue. I'm aiming to have the layout centered in the preview section. I attempted export const parameters = { layout: 'centered', }; in the .storybook/preview.js file However, this c ...

Failed deployment of a Node.js and Express app with TypeScript on Vercel due to errors

I'm having trouble deploying a Nodejs, Express.js with Typescript app on Vercel. Every time I try, I get an error message saying "404: NOT_FOUND". My index.ts file is located inside my src folder. Can anyone guide me on the correct way to deploy this? ...

What exactly does the $any() type cast in Angular do and how is it used?

While browsing the Angular.io site, I came across this title: The $any() type cast function There are instances where a binding expression may trigger a type error during AOT compilation and it is not possible or difficult to fully specify the type. To re ...

What is the best way to position Scroll near a mat row in Angular?

With over 20 records loaded into an Angular Material table, I am experiencing an issue where clicking on the last row causes the scroll position to jump to the top of the page. I would like the scroll position to be set near the clicked row instead. Is th ...

Unfiltered items remain unsorted when the checkbox is left unchecked

My goal is to create a filtering system for an array of four categories using separate checkboxes. Each click on a checkbox should filter the array by a specific category. I have implemented the code below to achieve this, but I am facing some issues. Curr ...

Fullstack is unable to locate the specified Entity name model

I am encountering an issue with my fullstack web application built using Angular and Spring Boot. When attempting to call my userEntity in the Angular service class via localhost:8080, I receive an error stating "Cannot find name 'UserEnt ...

Repeated calls to Angular's <img [src]="getImg()"> frequently occur

Can someone help me figure out what's going on here? Recently, I set up a new Angular project and in app.component.html, I have the following code: <img [src]="getDemoImg()"> In app.component.ts: getDemoImg(){ console.log('why so m ...