使用angularjs拦截器在工厂外实时发送数据

使用angularjs拦截器在工厂外实时发送数据,angularjs,httprequest,interceptor,Angularjs,Httprequest,Interceptor,我想解决如何在html中使用$scope.time。 我想在UI中显示最后一个http请求的时间,但给出了一个错误。 我不能使用$scope 我有以下代码: module.factory('timestamp', [function ($scope) { var time = { request: function (config) { $scope.time = new Date().getTime(); //wanted to show in UI realtim

我想解决如何在html中使用
$scope.time
。 我想在UI中显示最后一个http请求的时间,但给出了一个错误。 我不能使用
$scope

我有以下代码:

module.factory('timestamp', [function ($scope) { 
var time = {
    request: function (config) {

        $scope.time = new Date().getTime(); //wanted to show in UI realtime
        return config;
    }
    //,response: function (response) {
    //    response.config.responseTimestamp = new Date().getTime();
    //    return response;
    //}
};
return time;}];
配置:

module.config(function ($httpProvider) {
$httpProvider.interceptors.push('timestamp');}

请尝试返回$scope.time,或在任何返回的变量(而不是config)中获取时间 并尝试访问它

module.factory('timestamp', [function ($scope) { 
var time = {
    request: function (config) {

       config.requestTimestamp = new Date().getTime();
        return config;
    },
    response: function(response) {
        response.config.responseTimestamp = new Date().getTime();
        return response;
    }

};
return time;}];
控制器

module.config(function ($httpProvider) {
$httpProvider.interceptors.push('timestamp');}

请尝试返回$scope.time,或在任何返回的变量(而不是config)中获取时间 并尝试访问它

module.factory('timestamp', [function ($scope) { 
var time = {
    request: function (config) {

       config.requestTimestamp = new Date().getTime();
        return config;
    },
    response: function(response) {
        response.config.responseTimestamp = new Date().getTime();
        return response;
    }

};
return time;}];
控制器

module.config(function ($httpProvider) {
$httpProvider.interceptors.push('timestamp');}

这将在
return config
中出错,config必须是return,以便http请求将推送通过。我也看到了这一点。问题是如何从其他控制器访问。我仍然无法访问主控制器
module.controller('MainController',函数($scope,timestamp){console.log(timestamp.config);})中的配置/响应控制台.log(timestamp.request(config));log(timestamp.request());让我们看看。这将在
return config
中出错,config必须是return,以便http请求将推送通过。我也看到了这一点。问题是如何从其他控制器访问。我仍然无法访问主控制器
module.controller('MainController',函数($scope,timestamp){console.log(timestamp.config);})中的配置/响应控制台.log(timestamp.request(config));log(timestamp.request());让我们。