Я работаю над приложением angular 2 с картой открытого слоя. в этом мне нужно поделиться координатой местоположения от одного компонента к другим компонентам, используя службы общего доступа.
searchcomponents.ts
bounds: any;
placesChanged() {
var places = this.searchBox.getPlaces();
if (places == null) return;
if (places == undefined) return;
if (places.length == 0) return;
this.searchResultsSource.clear();
this.bounds = new google.maps.LatLngBounds();
// For each place, get the icon, name and location.
places.forEach(this.processPlace.bind(this));
// if the drawn result(s) is only one
if (this.searchResultsSource.getFeatures().length == 1) {
var singleFeature = this.searchResultsSource.getFeatures()[0];
this.pos = singleFeature.getGeometry().getCoordinates();
console.log(this.pos ,"hanumanh" );
this.service.setUrlHistoryObj(this.pos);
}
я хочу поделиться значением pos с другими компонентами. это мой служебный код
Search.services.ts
import {Injectable} from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class SearchService {
public pos : string;
public invokeEvent:Subject<any> = new Subject<string>();
constructor() {
this.pos="";
}
public setUrlHistoryObj(val: string): void {
this.pos = val;
console.log(val);
}
public getUrlHistoryObj(): string {
return this.pos;
}
в этом я получаю значение pos как геокоординату, и я хочу поделиться этим значением pos с другими компонентами.
Buffer.components.ts
value1: string;
constructor(
private urlHistoryService:SearchService) {
this.value1 = this.urlHistoryService.getUrlHistoryObj();
// this.value1 = this.urlHistoryService.setUrlHistoryObj();
}
я получаю ошибку undefined. пожалуйста, помогите мне.
@NgModule
, и служба является одноэлементной, я уже добавил в нее поставщиков: [SearchService]. - person Harshal   schedule 17.05.2017providers :[SearchService]
? Можете ли вы включить это в свой вопрос? - person eko   schedule 17.05.2017this
вconstructor
. т.е. дляBufferComponent
должно бытьthis.value1 = urlHistoryService.getUrlHistoryObj()
- person A. Tim   schedule 17.05.2017@NgModule({ imports: [ BrowserModule, HttpModule, MaterialModule, FormsModule ], declarations: [ AppComponent, RoiCreateComponent, SearchComponent, BufferDialogComponent, ], providers: [ RoiService, ArticleService, RoiDialogService, ArticleDialogService, FacebookService, AppService, LoginService, BufferDialogService ], entryComponents: [RoiDialogComponent, ArticleDialogComponent, BufferDialogComponent], bootstrap: [AppComponent] })
- person Harshal   schedule 17.05.2017@Component
и добавьте его в@NgModule
. - person eko   schedule 17.05.2017, providers:[SearchService], encapsulation: ViewEncapsulation.None })
- person Harshal   schedule 17.05.2017@components
и добавляю в@NgModule
и все работает. большое тебе спасибо - person Harshal   schedule 17.05.2017