A
A
ajky2016-12-15 14:48:24
Angular
ajky, 2016-12-15 14:48:24

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

2 answer(s)
C
Coder321, 2016-12-17
@ajky

Http uses an Observable which only fires once. If you need to update data use Subject.

V
Vitaly, 2016-12-15
@vitali1995

Code Dojo will help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question