I have 1 component that contains some projects. When I clicked on project I want to display him content in another component. How I can do this? Thanks.
Asked
Active
Viewed 565 times
-2
-
1Duplicate of [question](http://stackoverflow.com/questions/34088209/how-to-pass-object-from-one-component-to-another-in-angular-2) – Avnesh Shakya Dec 14 '16 at 17:07
1 Answers
0
There are several ways to do this.
Provide the "projects" objects via a service and inject that service into both components and use a route parameter via the angular2 router. You can then use the
ActivatedRoute
Param
s (import { ActivatedRoute, Params } from '@angular/router';
) to retrieve the "project" via the index of the "project" from the service.Create an global object the can hold a "project". Use a service to return an Observable to that object and inject it into both components. The you can store the set and retrieve the object from your observable.
Some combination of the two would be best depending on your exact needs
Code:
@Injectable()
export class ProjectsService {
getProjects(): Observable<Project[]> {
return Observable.of(PROJECTS);
}
}
const PROJECTS = [/*your projects here*/]
In short, you need to create a service to handle the data.