Configuring AngularDart Modules

一笑奈何 提交于 2019-11-28 02:15:32

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/19913222/configuring-angulardart-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!