Paylaşılan hizmetler, bileşenler arasındaki iletişim veya açısal 1 eski rootscope kavramının değiştirilmesi gibi pek çok durumu çözmek için en iyi uygulamadır. Benimkini oluşturmaya çalışıyorum ama Çalışma. Herhangi bir yardım ? ty !!!Köşeli 2 - Paylaşılan Hizmeti Kullanmak
bootstrap(AppComponent, [ SharedService ]);
: Başvurunuzu işe koşulması zaman
app.component.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){}
}
other.component.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;
}
}
Çok hızlısın: P İşte çalışan bir plunker: https://plnkr.co/edit/HX3LT9RaJD6ki3vpa1aW. Veriler bir dizide saklandığından, –
sakladığınız ayrı bilgi parçalarını görüntülemek için dizi üzerinde yinelemelisiniz, eğer yanılıyorsam düzeltin, ancak Angular 2 finalinde bootstrap değişmiş gibi görünüyor. Sağladığınız çözümü nasıl değiştirebilirim? Teşekkürler! – guicl
Gerçekten de, sağlanan bu çözüm Angular 5'in en son sürümü üzerinde çalışmıyor gibi görünüyor. Bootstrap sözdizimi, "Servis bir giriş bileşeni olarak kullanılamaz" denerseniz, değiştirildi. –