why should we use subscribe() over map() in Angular?

前端 未结 5 1499
花落未央
花落未央 2020-12-07 22:28

I am trying to take advantage of observables in angular2 and got confused on why should i use map() over subscribe(). Suppose i am getting values f

5条回答
  •  执笔经年
    2020-12-07 22:48

    Think map as a middleware which transforms the response.

    this.http.get('http://172.17.40.41:8089/api/Master/GetAllCountry')
    .map(r=>r.json())
     .subscribe(result => {
                  // here result would have json object that was parsed by map handler...
                },failurCallback,completeCallback)
    

    subscribe is used to invoke the observable, please read a good doc on cold-vs-hot-observables

提交回复
热议问题