I want the child component to access the shared service take the data and after injecting the child component to main component. I want the data of the sharedservice(rootscope) to directly put the data in mainComponents HTML, like here.
mainComponent.ts
import { Component } from '@angular/core';
import {ChildComponent} from './child';
import {AppServiceService} from './app-service.service';
@Component({
moduleId: module.id,
selector: 'rootscope-app',
templateUrl: 'rootscope.component.html',
styleUrls: ['rootscope.component.css'],
directives:[ChildComponent]
})
export class RootscopeAppComponent {
title = 'rootscope works!';
display:any;
constructor(appServiceService:AppServiceService){
this.display=appServiceService.getter();
}
}
sharedService.ts
import { Injectable} from '@angular/core';
@Injectable()
export class AppServiceService {
ser = "hello i am from service";
public data: any;
constructor() {
}
settter(data: any) {
this.data = data;
}
getter() {
return this.data;
}
}
childComponent of mainComponent
import { Component, OnInit } from '@angular/core';
import {AppServiceService} from '../app-service.service'
@Component({
moduleId: module.id,
selector: 'app-child',
templateUrl: 'child.component.html',
styleUrls: ['child.component.css']
})
export class ChildComponent implements OnInit {
dispaly: string;
constructor(appServiceService: AppServiceService) {
this.dispaly = "Child component binding...";
appServiceService.settter(this.dispaly);
}
ngOnInit() {}
}
$rootScope and $scope both are not available in Angular2. You can think to use service (shareService) and inject it into boostrap function. This way you will be able to share data throughout application(HTML as well).
Look at here. http://plnkr.co/edit/7A21ofKNdi0uvbMgLUDZ?p=preview
sharedService
Component
Given the desired output is that we want to use shared values inside any template, then using a service is not an answer.
The solution is making a base class to all components, where everything we need globally resides.
create a class, name it
BaseComponent
and make every component extends it
and inside its HTML template
or alternatively if you want a change in the view (template) to be fired from a service function call, you can place a callback in the service and assign it from the component so it will be fired whenever the service function called, take this case as an example
In my case I wanted to show a message that can be fired from any component, so I placed it in the
app.component.html
which survives along the app lifetimeinside the
app.component.ts
the
MainService
is the service that contains the callback, as you see belownow from any component, with the main service registered in the constructor, you can show the message as follows
it is possible by using angular BehaviorSubject and asObservable
Check below example code
Service file
Component
Set the data from any component
Listen from any component
You can communicate between any component using this way.
More info and other methodes