Question

I'm trying to add an HttpInterceptor. In AngularJS I would write something like this:

m.config(["$httpProvider", function($httpProvider) {...}])

But it seems like there is no config function in AngularDart.

What is the right way to configure AngularDart modules?

Was it helpful?

Solution

HttpInterceptors are implemented slightly differently in AngularDart.

main() {
  // define your interceptor
  var intercept = new HttpInterceptor();
  intercept.request = (HttpResponseConfig requestConfig) => /* something */;
  intercept.response = (HttpResponse response) => /* something */;
  intercept.requestError = (dynamic error) => /* something */;
  intercept.responseError = (dynamic error) => /* something */;

  // get hold of the HttpInterceptors instance -- there are many ways to do this.
  Injector injector = ngBootstrap(/* ... */);
  var interceptors = injector.get(HttpInterceptors);

  // register/add your interceptor
  interceptors.add(intercept)
}

More info on the API:

http://ci.angularjs.org/view/Dart/job/angular.dart-master/javadoc/angular.core.dom/HttpInterceptors.html http://ci.angularjs.org/view/Dart/job/angular.dart-master/javadoc/angular.core.dom/HttpInterceptor.html

OTHER TIPS

The answer by @pavelgj is not entirely correct way to use HttpInterceptors. It can introduce hard to debug timing errors, when certain Http calls would have been already initiated by bootstrapped angular-dart app, hence the interceptor would miss those calls.

The right way to use interceptor is to make sure that you inject it before the first Http call somewhere within your angular app. The new syntax of bootstrapping is this:

applicationFactory()
  .addModule(new YourAngularApp())
  .run();

So within YourAngularApp, wherever you are injecting Http for the first time, you can inject the HttpInterceptor (only once) and add your custom interceptor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top