developthewebz
developthewebz

Reputation: 1937

RXJS and Angular 1

I'm trying to play around with RxJS and Angular 1. I'm running into an issue where I can't call .done(), .fail() or .always() on an $http request. Is it possible to use RxJS with the $http service in Angular 1?

This is how I have my observable set up so far:

angular.module('rxExampleApp')
  .controller('Step3Ctrl', function ($http) {
    var vm = this;
    vm.requestStream = Rx.Observable.just('https://api.github.com/users');

    vm.requestStream.subscribe(function (requestUrl) {
      // execute the request

      vm.responseStream = Rx.Observable.create(function (observer) {
        $http.get(requestUrl)
          .done(function (response) {
            observer.onNext(response);
          })
          .fail(function (jqXHR, status, error) {
            observer.onError(error);
          })
          .always(function () {
            observer.onCompleted();
          });
      });
    });

    vm.responseStream.subscribe(function (response) {
      console.log('TEST RESPONSE: ', response);
    });
  });

But I'm running into an error where $http.get.done is not a function. Is what I am trying to accomplish possible in Angular 1.4 with RxJS?

Upvotes: 2

Views: 562

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

done, fail & always these methods are available there on jQuery Ajax, not on angular $http method's.

$http.get doesn return a promise, and you could have .then function over it. So you could place success, error & completed callback.

Code

$http.get(requestUrl)
 .then(function (response) { //success function
        observer.onNext(response);
  }, function (error) { //error function
     observer.onError(error);
  }, function () { //completed callback
     observer.onCompleted();
  }
);

Upvotes: 1

Related Questions