我希望子组件访问共享服务并在将子组件注入主组件之后获取数据。我想让sharedservice(rootscope)的数据直接把数据放到mainComponents HTML中,就像这里。
主组件.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();
}
}
Run Code Online (Sandbox Code Playgroud)
共享服务.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; …Run Code Online (Sandbox Code Playgroud) angular ×1