Eckige 2 - Verwendung Von Shared Service

Sieht aus wie shared services ist die beste Praxis, lösen viele Situationen wie die Kommunikation zwischen Komponenten oder als Ersatz der alten $rootscope Konzept des Winkel-1. Ich versuche zu schaffen, mine, aber es funktioniert nicht. Keine Hilfe ? ty !!!

app.Komponente.ts

import {Component} from 'angular2/core';
import {OtherComponent} from './other.component';
import {SharedService} from './services/shared.service';
@Component({
selector: 'my-app',
providers: [SharedService],
directives: [OtherComponent],
template: `
    <button (click)="setSharedValue()">Add value to Shared Service</button>
    <br><br>
    <other></other>
`
})
export class AppComponent { 
data: string = 'Testing data';
setSharedValue(){
    this._sharedService.insertData(this.data);
    this.data = '';
    console.log('Data sent');
}
constructor(private _sharedService: SharedService){}
}

anderen.Komponente.ts

import {Component, OnInit} from "angular2/core";
import {SharedService} from './services/shared.service';
@Component({
selector : "other",
providers : [SharedService],
template : `
I'm the other component. The shared data is: {{data}}
`,
})
export class OtherComponent implements OnInit{
data: string[] = [];
constructor(private _sharedService: SharedService){}
ngOnInit():any {
    this.data = this._sharedService.dataArray;
}
}
InformationsquelleAutor Marco Jr | 2016-03-27
Schreibe einen Kommentar