拦截器和 ng-bind
Interceptors and ng-bind
我正在学习拦截器。我在控制台上“打印”结果,但我想在屏幕上显示它们,例如 {{}} 或使用 ng-bind。我试过了,但我做不到。
有人能帮帮我吗?
x.factory("inter", ["$q", function($q) {
return {
request: function(config) {
console.log("Request: " + JSON.stringify(config));
return config;
}
};
}]);
x.config(["$httpProvider", function($httpProvider) {
$httpProvider.interceptors.push("inter");
}]);
谢谢!
将您的代码替换为
x.factory("inter", ["$q", function($q) {
var configs = [];
return {
request: function(config) {
configs.push("Request: " + JSON.stringify(config));
return config;
},
interceptedConfigs: configs
};
}]);
x.config(["$httpProvider", function($httpProvider) {
$httpProvider.interceptors.push("inter");
}
然后,在控制要打印配置的视图的控制器中,注入拦截器,并将其配置公开给作用域:
x.controller('SomeCtrl', function($scope, inter) {
$scope.interceptedConfigs = inter.interceptedConfigs;
});
然后在该控制器的视图中:
{{ interceptedConfigs }}
我正在学习拦截器。我在控制台上“打印”结果,但我想在屏幕上显示它们,例如 {{}} 或使用 ng-bind。我试过了,但我做不到。
有人能帮帮我吗?
x.factory("inter", ["$q", function($q) {
return {
request: function(config) {
console.log("Request: " + JSON.stringify(config));
return config;
}
};
}]);
x.config(["$httpProvider", function($httpProvider) {
$httpProvider.interceptors.push("inter");
}]);
谢谢!
将您的代码替换为
x.factory("inter", ["$q", function($q) {
var configs = [];
return {
request: function(config) {
configs.push("Request: " + JSON.stringify(config));
return config;
},
interceptedConfigs: configs
};
}]);
x.config(["$httpProvider", function($httpProvider) {
$httpProvider.interceptors.push("inter");
}
然后,在控制要打印配置的视图的控制器中,注入拦截器,并将其配置公开给作用域:
x.controller('SomeCtrl', function($scope, inter) {
$scope.interceptedConfigs = inter.interceptedConfigs;
});
然后在该控制器的视图中:
{{ interceptedConfigs }}