-
-
Save cjonagam/81c985cb8c29faf94d90 to your computer and use it in GitHub Desktop.
Angular Interceptor Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Angular JS</title> | |
</head> | |
<body ng-app="jsbin"> | |
<div ng-controller="DemoCtrl as demo"> | |
<h1>Hello {{demo.name}}</h1> | |
<button ng-click='test()'>tst</button> | |
</div> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.js"></script> | |
<script id="jsbin-javascript"> | |
var app = angular.module('jsbin', []); | |
app.controller('DemoCtrl', function($scope,$http) { | |
this.name = 'World'; | |
$scope.test=function(){ | |
$http.get('https://api.github.com/users/naorye/repos').then(function(response) { | |
var time = response.config.responseTimestamp - response.config.requestTimestamp; | |
console.log('The request took ' + (time / 1000) + ' seconds.'); | |
}); | |
} | |
}); | |
app.factory('authCheck', [function() { | |
var timestampMarker = { | |
request: function(config) { | |
config.requestTimestamp = new Date().getTime(); | |
return config; | |
}, | |
response: function(response) { | |
response.config.responseTimestamp = new Date().getTime(); | |
return response; | |
} | |
}; | |
return timestampMarker; | |
}]); | |
app.config(['$httpProvider', function($httpProvider) { | |
$httpProvider.interceptors.push('authCheck'); | |
}]); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Angular JS</title> | |
</head> | |
<body ng-app="jsbin"> | |
<div ng-controller="DemoCtrl as demo"> | |
<h1>Hello {{demo.name}}</h1> | |
<button ng-click='test()'>tst</button> | |
</div> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.js"><\/script> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">var app = angular.module('jsbin', []); | |
app.controller('DemoCtrl', function($scope,$http) { | |
this.name = 'World'; | |
$scope.test=function(){ | |
$http.get('https://api.github.com/users/naorye/repos').then(function(response) { | |
var time = response.config.responseTimestamp - response.config.requestTimestamp; | |
console.log('The request took ' + (time / 1000) + ' seconds.'); | |
}); | |
} | |
}); | |
app.factory('authCheck', [function() { | |
var timestampMarker = { | |
request: function(config) { | |
config.requestTimestamp = new Date().getTime(); | |
return config; | |
}, | |
response: function(response) { | |
response.config.responseTimestamp = new Date().getTime(); | |
return response; | |
} | |
}; | |
return timestampMarker; | |
}]); | |
app.config(['$httpProvider', function($httpProvider) { | |
$httpProvider.interceptors.push('authCheck'); | |
}]); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('jsbin', []); | |
app.controller('DemoCtrl', function($scope,$http) { | |
this.name = 'World'; | |
$scope.test=function(){ | |
$http.get('https://api.github.com/users/naorye/repos').then(function(response) { | |
var time = response.config.responseTimestamp - response.config.requestTimestamp; | |
console.log('The request took ' + (time / 1000) + ' seconds.'); | |
}); | |
} | |
}); | |
app.factory('authCheck', [function() { | |
var timestampMarker = { | |
request: function(config) { | |
config.requestTimestamp = new Date().getTime(); | |
return config; | |
}, | |
response: function(response) { | |
response.config.responseTimestamp = new Date().getTime(); | |
return response; | |
} | |
}; | |
return timestampMarker; | |
}]); | |
app.config(['$httpProvider', function($httpProvider) { | |
$httpProvider.interceptors.push('authCheck'); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment