Skip to content

Commit 0072962

Browse files
committed
chore: Add examples of component communication methods in README.md
1 parent e6f018e commit 0072962

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

README.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,208 @@ export class ParentComponent {
737737

738738
[Back to top⤴️](#table-of-contents)
739739

740+
## Siblings to Siblings Communication
741+
742+
**Using Services** - Services are a way to share data and functionality between components in Angular. You can create a service that holds the data and methods that need to be shared between components.
743+
744+
Example 1 :
745+
746+
```typescript
747+
import { Injectable } from '@angular/core';
748+
749+
@Injectable({
750+
providedIn: 'root'
751+
})
752+
753+
export class DataService {
754+
message: string;
755+
756+
setMessage(message: string) {
757+
this.message = message;
758+
}
759+
760+
getMessage() {
761+
return this.message;
762+
}
763+
}
764+
```
765+
766+
```typescript
767+
import { Component } from '@angular/core';
768+
import { DataService } from './data.service';
769+
770+
@Component({
771+
selector: 'app-sibling1',
772+
templateUrl: './sibling1.component.html',
773+
styleUrls: ['./sibling1.component.css']
774+
})
775+
776+
export class Sibling1Component {
777+
message: string;
778+
779+
constructor(private dataService: DataService) {}
780+
781+
sendMessage() {
782+
this.dataService.setMessage('Hello from sibling1 component');
783+
}
784+
}
785+
```
786+
787+
```html
788+
<!--sibling1.component.html-->
789+
<button (click)="sendMessage()">Send Message</button>
790+
```
791+
792+
```typescript
793+
import { Component } from '@angular/core';
794+
import { DataService } from './data.service';
795+
796+
@Component({
797+
selector: 'app-sibling2',
798+
templateUrl: './sibling2.component.html',
799+
styleUrls: ['./sibling2.component.css']
800+
})
801+
802+
export class Sibling2Component {
803+
message: string;
804+
805+
constructor(private dataService: DataService) {}
806+
807+
receiveMessage() {
808+
this.message = this.dataService.getMessage();
809+
}
810+
}
811+
```
812+
813+
```html
814+
<!--sibling2.component.html-->
815+
<p>{{ message }}</p>
816+
```
817+
818+
Example 2 :
819+
820+
**Using RxJS Subjects** - RxJS Subjects are a way to share data and events between components in Angular. You can create a Subject that emits events and subscribe to those events in the components.
821+
822+
```typescript
823+
import { Injectable } from '@angular/core';
824+
import { Subject } from 'rxjs';
825+
826+
@Injectable({
827+
providedIn: 'root'
828+
})
829+
830+
export class DataService {
831+
message = new Subject<string>();
832+
833+
setMessage(message: string) {
834+
this.message.next(message);
835+
}
836+
}
837+
```
838+
839+
```typescript
840+
import { Component } from '@angular/core';
841+
import { DataService } from './data.service';
842+
843+
@Component({
844+
selector: 'app-sibling1',
845+
templateUrl: './sibling1.component.html',
846+
styleUrls: ['./sibling1.component.css']
847+
})
848+
849+
export class Sibling1Component {
850+
message: string;
851+
852+
constructor(private dataService: DataService) {}
853+
854+
sendMessage() {
855+
this.dataService.setMessage('Hello from sibling1 component');
856+
}
857+
}
858+
```
859+
860+
```html
861+
<!--sibling1.component.html-->
862+
<button (click)="sendMessage()">Send Message</button>
863+
```
864+
865+
```typescript
866+
import { Component } from '@angular/core';
867+
import { DataService } from './data.service';
868+
869+
@Component({
870+
selector: 'app-sibling2',
871+
templateUrl: './sibling2.component.html',
872+
styleUrls: ['./sibling2.component.css']
873+
})
874+
875+
export class Sibling2Component {
876+
message: string;
877+
878+
constructor(private dataService: DataService) {}
879+
880+
ngOnInit() {
881+
this.dataService.message.subscribe(message => {
882+
this.message = message;
883+
});
884+
}
885+
}
886+
```
887+
888+
```html
889+
<!--sibling2.component.html-->
890+
<p>{{ message }}</p>
891+
```
892+
893+
Example 3 :
894+
895+
**Using ViewChild and ViewChildren** - ViewChild and ViewChildren are a way to access child components in Angular. You can use ViewChild to access a single child component and ViewChildren to access multiple child components.
896+
897+
```typescript
898+
import { Component, ViewChild } from '@angular/core';
899+
import { Sibling2Component } from './sibling2.component';
900+
901+
@Component({
902+
selector: 'app-sibling1',
903+
templateUrl: './sibling1.component.html',
904+
styleUrls: ['./sibling1.component.css']
905+
})
906+
907+
export class Sibling1Component {
908+
@ViewChild(Sibling2Component) sibling2: Sibling2Component;
909+
910+
sendMessage() {
911+
this.sibling2.message = 'Hello from sibling1 component';
912+
}
913+
}
914+
```
915+
916+
```html
917+
<!--sibling1.component.html-->
918+
<button (click)="sendMessage()">Send Message</button>
919+
```
920+
921+
```typescript
922+
import { Component } from '@angular/core';
923+
924+
@Component({
925+
selector: 'app-sibling2',
926+
templateUrl: './sibling2.component.html',
927+
styleUrls: ['./sibling2.component.css']
928+
})
929+
930+
export class Sibling2Component {
931+
message: string;
932+
}
933+
```
934+
935+
```html
936+
<!--sibling2.component.html-->
937+
<p>{{ message }}</p>
938+
```
939+
940+
[Back to top⤴️](#table-of-contents)
941+
740942
## Data binding
741943

742944
Data binding is a core feature of Angular that allows you to bind data between the component's class and the HTML template. There are two types of data binding in Angular:

0 commit comments

Comments
 (0)