LoginSignup
5
5

More than 5 years have passed since last update.

AngularJS公式にあるサンプルコードが動かない

Last updated at Posted at 2014-11-12

AngularJS公式サイトにあるInterceptorsの下記サンプルコードコピペして利用するとエラーが発生!

interceptors
// alternatively, register the interceptor via an anonymous factory
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
  return {
   'request': function(config) {
       // same as above
    },

    'response': function(response) {
       // same as above
    }
  };
});

ググって辿りつき同じように困っている人をStackOverFlowで発見、
そこでの回答は、

This is the damned code I copied without much thinking

のろわれたコード 」とい文字が・・・やっぱり動かないのね。
公式にあるサンプルコードだから動くのが当たり前と思ったけど、動かないこともあるんですね・・・
もしかしたら何かが影響してて動かないのかな。
わかる方がいたら教えて下さい―

ちなみに、もうひとつある下記サンプルコードはちゃんと動きました!

interceptors
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config;
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    },



    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      if (canRecover(rejection)) {
        return responseOrNewPromise
      }
      return $q.reject(rejection);
    }
  };
});

$httpProvider.interceptors.push('myHttpInterceptor');
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5