【发布时间】:2017-03-31 22:17:25
【问题描述】:
我的应用程序 80% 是用 Angular 编写的,20% 是用 jquery 编写的。我已经使用 $httpprovider 在 Angular 中编写了一个请求拦截器,并且它可以与其他 Angular 页面一起正常工作。 我有三个问题:
1) 我想为我的 jquery 页面使用相同的拦截器。我怎样才能做到这一点?
2) 我希望我的拦截器在页面加载时只被调用一次。我怎样才能做到这一点?它目前被调用了 7-8 次(我猜是整个页面加载期间的 ajax 调用次数)。
3) 谁能给我输入我如何为这个拦截器和使用这个拦截器的页面编写 jasmine 规范。 提前致谢!
app.config(['$httpProvider', function ($httpProvider) {
'use strict';
$httpProvider.interceptors.push('myAppInterceptor');
}]);
app.factory('myAppInterceptor', ['$q','$window','$injector',function ($q,$window,$injector) {
'use strict';
var myAppInterceptor = {
request: function(config) {
console.log('myAppInterceptor is called');
// some business logic done here...
}
return config;
}
};
return myAppInterceptor;
}]);
【问题讨论】:
标签: javascript jquery angularjs