I'm using angular 4,im not able to communicate with component.I have sidenav component and I want to share values sidebar to main component. Thanks.
Asked
Active
Viewed 65 times
0
-
Please share you code and what you're trying to attempt, what is not working and error messages. – Capricorn Jul 26 '18 at 14:11
-
3[Angular Component Interaction](https://angular.io/guide/component-interaction) – domfx Jul 26 '18 at 14:12
-
1Possible duplicate of [How do I share data between components in Angular 2?](https://stackoverflow.com/questions/31026886/how-do-i-share-data-between-components-in-angular-2) – René Winkler Jul 26 '18 at 14:12
3 Answers
0
There's many ways. For example:
- @Output/@Input
- Service
In fact, there's even a whole section in Angular docs devoted to it:

David Anthony Acosta
- 4,766
- 1
- 19
- 19
0
You can create a service and add it to providers at app level (root) this way the service will be available to inject to all the component and any change on this service will be persist.
@Injectable()
export class GlobalSharedService {
public data:any;
}
app.module
@NgModule({
....
providers: [GlobalSharedService]
})
export class AppModule { }
In Angular 6 you can add the service to proviers at the app.module level like this
@Injectable({
providedIn: 'root',
})
export class GlobalSharedService {
public data:any;
}
All service added at the app.module the created once that why the persist there state and called singleton service

Muhammed Albarmavi
- 23,240
- 8
- 66
- 91
0
sidenav.component.ts
passData:any
sidenav.component.html
//Call your main component & share passData property
<app-component [passData]="passData"></app-component>
main.component.ts
@ViewChild(passData) passData: SidenavComponent;

Rahul Dapke
- 345
- 6
- 19