In Angular2 you use shared services instead of $scope
https://angular.io/docs/ts/latest/cookbook/component-communication.html
@Injectable()
class SharedService {
someState:String;
someEventStream:Subject<String> = new Subject<String>();
}
@Component({
selector: ...,
providers: [SharedService]
})
class MyComponent {
constructor(private sharedService:SharedService) {}
}
For each provider ([SharedService]
is short for [{provide: SharedService, useClass: SharedService}]
) in above example a single instance is maintained.
The scope of such a provider is the component where it is provided and it's descendants when no descendant has the same provider registered.
When a component requests a dependency constructor(private sharedService:SharedService)
, Angulars DI (dependency injection) starts looking at the components own providers and then upwards towards the root component and then the providers registered at bootstrap. It returns the first one it finds.
If two components got the same instance (from the same provider) injected, then they can communicate using this service by subscribing to and emitting events of the Subject
or reading and writing the state or by calling methods on the service.