Home > Net >  How to add/update data property while navigating progamitcally in Angular?
How to add/update data property while navigating progamitcally in Angular?

Time:08-06

I want to add data to data property of the route while progamitically navigating to a dynamic route. Need for this is that I don't want show data i am passing in URL

this.router.navigate([`${url}/${dynamicId}/view`])

In navigation extras there is no attribute for data. Can you please help me out how to achieve this.

CodePudding user response:

You can use the data/state attribute of the NavigationExtras :)

The entire mechanism and implementation details you will find here: indepth-guide-to-passing-data-via-routing

The short answer tho is the following. In the component that makes the navigation, add state to the extras:

    const state = {dynamicId: 'your-hidden-id'};
    this.router.navigate(['your-route'], { state });

then in the component you are redirecting to, you have to use currentNavigation object to get the dynamic id, as follows:

    constructor(
        private router: Router,
    ) {
        const state = this.router.getCurrentNavigation().extras.state;
        this.dynamicId = state['dynamicId'];
    }

  • Related