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() {}
}