我想,當一個變量發生變化,所以它似乎是觀察到這裏去 的方式來觸發一個事件是我的服務:認購不觸發
import { Injectable } from '@angular/core';
import {Ad} from './../ad/ad';
import {Observable} from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
@Injectable()
export class SearchResultService {
public ads : Ad[] = [];
getAdsObservable(): Observable<Ad[]> {
return Observable.of(this.ads);
}
}
和組件:
import {Component } from '@angular/core';
import {Ad} from './../ad/ad';
import {SearchResultService} from './../search/searchResult.service';
import {Observable} from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'map',
template : '
<div #map id="map" class='main_map' ></div>
',
})
export class mapComponent {
obs;
subscription: Subscription;
constructor(
private searchResultService: SearchResultService,
){
this.obs = this.searchResultService.getAdsObservable();
}
ngOnInit(){
this.subscription = this.obs.subscribe(
value => {
console.log('subscribe trigger');
});
}
}
現在的問題是,當searchResultService.ads變化(基本全新廣告加載),它不會觸發認購
工作,我相信你正在尋找一個rxjs主題(https://github.com/Reactive-Extensions/RxJS/blob /master/doc/gettingstarted/subjects.md)這允許您將值推送給主題,訂閱它的任何內容都將監聽更改。 – LLai