0

I have two components in my application - MasterComponent and DetailsComponent

On clicking an option in MasterComponent, it is routed to DetailsComponent, also I need to pass some dynamic objects to DetailsComponent. As far as I understand there are few ways to achieve this

1] Using router params

I can't pass objects using router params, only strings because it needs to be reflected in the URL.

2] Using shared service

We can pass complex objects through a shared service but on reloading the page the data in the service is cleared. I need data to be persisted on page reload.

Is there any other way to pass dynamic complex data to routed Component? Any help is appreciated. Thanks.

2 Answers 2

2

I would use a shared service. Is really the best approach in my humble opinion.

Check this: https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service

The example there is very self explanatory! :D

The service works with listeners, so you subscribe to it. And when both components are subscribed to it the flow of data is consistent and fluid. I used this approach myself to communicate between my back end server and 3 components in my App.

My data flow is the following:

  1. Component A receives data from server. Stores it into an object and calls a method in a shared service passing this object.
  2. Component B listens to this method and receives the data passed from Component A to the shared service.
  3. Same approach with Component C.

Is this flow what you are looking for?

Update (saving data when reloading) :

Storing data options:

  1. LocalStorage
  2. Cookie
  3. Session

Links:

Hope it helped! Good coding!

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. As I mentioned in the question, if we use a shared service, data is cleared on reloading the page. Do you have any solution to this problem?
@AbhishekV I am not reloading the page almost never, but when I do I repeat the process: 1. Get data from Server. 2. Pass it to rest of the Components through shared service. This helps me to have 1 Component that gathers the data from the Server and manages it, and then sends it to the DataTables component (for example). So, I just make 1 call to server and manage at its best the data.
@AbhishekV Just have a Component A to gather your data, manage it and put it into an object. And pass that object throughout all your App using a shared service. As soon as you get used to it will be your best friend when passing data from one component to another.
But what if the data is not coming from server, if user has submitted the data through from then how we can persist it?
@AbhishekV Well, you can always pass data using @Input() but, when reload you lose it. So in the case that you want to maintain data when refreshing/reloading, you will need to store it: local json, cache, etc. I will update my answer with some useful links for this matter
|
0

You can do so using Resolve; say you need to get user infos from your database through one of your services and need to display it in your details-component.

Resolving route datas will enable you to fetch datas before activating the selected route.

In your route config:

{
  path: 'user/:id',
  component: 'UserDetailComponent',
  resolve: {
    userProfile: "UserProfileResolverService"
  }
}

Your UserProfileResolverService would be:

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from "@angular/router";
import { User } from "path";
import { UserService } from "path to your service";

@Injectable()
export class UserProfileResolverService implements Resolve<any>{

  constructor(private userService: UserService) { }

  resolve(route: ActivatedRouteSnapshot){

    return this.userService.getUser(route.paramMap.get('id'))
        .toPromise()
        .then(res =>{
          return res.json().data as User[];
        });

  }

}

For way more infos about resolving route datas read the excellent article by Pascal Precht: https://blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html

3 Comments

Thanks for the answer. Is it possible to pass form data submitted in one component to another content through this method?
To communicate between components use Events and @Output(): toddmotto.com/component-events-event-emitter-output-angular-2
But @Input and @Output can be used only if one component is child of another component right. I don't think it can be used in case of routing

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.