0

I am working in angular4 project.In the project,I need to display list of data in a component in cards format.When user click on a card,I need to navigate the user to details page of that particular card.For that one,I am passing params in router navigate containing id and name.My problem is,in the details page URL,card name is displaying along with id.But I need to display only name and have to hide id.I have tried location.replaceState.It doesnot works on page refreshing,as I need id for getting data of the card.Is there any other ways for hiding particular data(id) in the URL in angular2?

srujana
  • 433
  • 1
  • 10
  • 33

2 Answers2

0

Probably what you are looking for is, you want the id based on the route/:name inside the component, that is invoked after the route.

You can try this way :

Your route path :

 { path: 'home/:name', component: MyComponent, data {id : 2} },

component file

constructor (private _router : Router, private _route : ActivatedRoute)

 this._router.events
    .filter(event => event instanceof NavigationEnd)
    .subscribe(
        () => {
            console.log(this._route.snapshot.data) // should print {id : 2}
        }
);
Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
0

Here is the Example for you hide and pass your data to other page iam allready explained to someone and marked as answer.

Passing a variable between two components without inheritance angular 2

pls visit it is useful for you to achieve your logic.

Happy codding.

Karnan Muthukumar
  • 1,843
  • 1
  • 8
  • 15
  • Thanks for the answer.I have tried by using service.But on sharing url to someone,the data in service or app component variable will be cleared so,the other person will not get any data.In this case (sharing url), how can I store the value permanently? – srujana Jun 08 '18 at 13:00
  • local storage is available that one not cleared temporarily. pls try this one – Karnan Muthukumar Jun 08 '18 at 13:02
  • pls visit this link i have answer about local storage.. https://stackoverflow.com/questions/40589730/local-storage-in-angular-2/46562398#46562398 – Karnan Muthukumar Jun 08 '18 at 13:04
  • location storage is limited to one system.But if the shared url is opened in another system,then location storage won't work right? – srujana Jun 08 '18 at 13:07
  • yes not shared one system to another system you are right. But your question is "Is there any other ways for hiding particular data(id) in the URL in angular2" – Karnan Muthukumar Jun 08 '18 at 13:09
  • thats why iam replied possible things of related to your question. – Karnan Muthukumar Jun 08 '18 at 13:10
  • ok.I found different solutions for my question and raised with this different constraints – srujana Jun 08 '18 at 13:24