0

exactly what the title suggests is my question. I will start by explaining what i have to do.

So I have a component widget. When I click on it a modal pops containing couple of items. Each item represents a different section in the main application. On click on any item a specific section will be displayed related to that item (which is a component itself).

I have no previews experience with Angular 1 or 2 so to be honest I have no idea where to start from or what should I search for. The best I come out with is EventEmitter, Output, Input (but those are for child-parent / parent-child communication from what i've read, which is not the case). Also I've been looking on Observable but i don't think this is helpful in this situation (for what i've understand i deals with server side service calls). If you had any experience please share. Thank you.

1

2 Answers 2

2

The code below demonstrates the communication between sibling components, parent and child components with the help of angular services.

app.module.ts

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { ParentComponent } from './app.parent.component'; import { ChildComponent } from './child.component'; @NgModule({ imports: [ BrowserModule, FormsModule ], declarations: [ ParentComponent, ChildComponent ], bootstrap: [ ParentComponent ] }) export class AppModule {} 

app.parent.component.ts

import { Component } from '@angular/core'; import { ParentChildService } from './parent-child.service'; @Component({ selector: 'parent-app', template: ` <div> <div> <h1>Hello from Parent</h1> </div> <div style="border: 1px dotted blue;"> <p> <b> Child message history </b> </p> <p *ngFor="let message of childMessages"> {{ message }} </p> </div> <child-app [id] = 1> </child-app> <child-app [id] = 2> </child-app> </div> `, providers: [ ParentChildService ] }) export class ParentComponent { childMessages: string[] = []; constructor(private parentChildService: ParentChildService) { parentChildService.attentionRequested$.subscribe((request) => { this.childMessages.push(this.parentChildService.requestedBy + ": " + request); }); } } 

child.component.ts

import { Component, OnDestroy, Input } from '@angular/core'; import { ParentChildService } from './parent-child.service'; import { Subscription } from 'rxjs/Subscription'; @Component({ selector: 'child-app', template: ` <div> <div> <h2>Hello from {{ id }} child component</h2> <span> Message: </span> <input type="text" [(ngModel)]="message"> <button (click) = "requestAttention();"> Request </button> </div> <div style="border: 1px dotted green;"> <p> <b> Sibling message history </b> </p> <p *ngFor="let message of siblingMessages"> {{ message }} </p> </div> </div> ` }) export class ChildComponent implements OnDestroy { message: string; subscription: Subscription; siblingMessages: string[] = []; @Input() id: number; requestAttention(): void { this.parentChildService.requestAttention(this.message, this.id); } constructor(private parentChildService: ParentChildService) { this.subscription = parentChildService.attentionRequested$.subscribe((request, id) => { if (this.id != this.parentChildService.requestedBy) { this.siblingMessages.push(this.parentChildService.requestedBy + ": " + request); } }); } ngOnDestroy(): void { // prevent memory leak when component destroyed this.subscription.unsubscribe(); } } 

parent-child.service.ts

import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable() export class ParentChildService { // Observable sources private requestAttentionSource = new Subject<string>(); // Observable streams attentionRequested$ = this.requestAttentionSource.asObservable(); requestedBy: number; requestAttention(request: string, id: number) { this.requestedBy = id; this.requestAttentionSource.next(request); } } 

The gist demonstrates the same thing.

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

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
0

I'd use a singleton to hold the state, and inject it everywhere I need to read the state/change the state.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.