Last Updated: February 25, 2016
·
1.236K
· lperrin

Tracing calls in AngularJS

I recently had a problem where I would sometimes pass null URLs to $http.get. With promises all over the place, it's not always easy to trace where the bad data comes from.

That is, until you start playing with decorators. For example, you can log all HTTP requests with:

ngApp.config(function ($provide) {
  $provide.decorator('$http', function ($delegate) {
    var _get = $delegate.get;

    $delegate.get = function () {
      console.log.apply(console, arguments);

      return _get.apply(this, arguments);
    };

    return $delegate;
  });
});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.

Security insights from Cside