Answer the question
In order to leave comments, you need to log in
How Observable/Observer works and how to update values through it in Angular2?
1. Do I understand correctly that functions dependent on a function with an Observable should receive values after it is called/changed?
2. And how to trigger updates in Angular2 for functions dependent on Observable?
@Injectable()
export class StockService(){
constructor (private http: Http){
}
getStocks(): Observable<any>{
return this.http.get("http://localhost:3000/stocks")
.map( (res: Response) => res.json() )
.catch((error:any) => Observable.throw(error.json().error || ' Server Error '));
}
}
import {StockService} from '../stock.service';
@Component({
selector: 'stocks',
providers: [ StockService ],
template: `<ul>
<li*ngFor="let stock of stocks">
</ul>`
})
export class StockComponent(){
stocks: string[];
constructor (private stockService: StockService) {}
getAllStocks(){
this.stockService.getStocks()
.subscribe(
data => this.stocks = data ,
error => console.log('Server Error')
);
}
}
Answer the question
In order to leave comments, you need to log in
Http uses an Observable which only fires once. If you need to update data use Subject.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question