You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+202Lines changed: 202 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -737,6 +737,208 @@ export class ParentComponent {
737
737
738
738
[Back to top⤴️](#table-of-contents)
739
739
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
+
exportclassDataService {
754
+
message:string;
755
+
756
+
setMessage(message:string) {
757
+
this.message=message;
758
+
}
759
+
760
+
getMessage() {
761
+
returnthis.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
+
exportclassSibling1Component {
777
+
message:string;
778
+
779
+
constructor(privatedataService:DataService) {}
780
+
781
+
sendMessage() {
782
+
this.dataService.setMessage('Hello from sibling1 component');
**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
+
exportclassDataService {
831
+
message =newSubject<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
+
exportclassSibling1Component {
850
+
message:string;
851
+
852
+
constructor(privatedataService:DataService) {}
853
+
854
+
sendMessage() {
855
+
this.dataService.setMessage('Hello from sibling1 component');
**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.
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