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() {}
}
The controllers and $scope in Angular 1 have been replaced with “Components” in Angular 2. Hence we can say that it is a component-based framework, which uses zone.
In Angular 2, controllers and $scope were replaced by components and directives. Components are directives with a template.
All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.
The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).
$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
bootstrap(App, [sharedService]);
sharedService
import {Injectable} from 'angular2/core'
@Injectable()
export class sharedService {
name="micronyks";
}
Component
@Component({
selector: 'thecontent',
template: `
<h1>Component II - {{ss.name}} </h1>
`
})
export class TheContent {
constructor(private ss: sharedService) {
console.log("content started");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With