Angularjs 拦截器和ng绑定
我正在学习拦截器。我正在控制台上“打印”结果,但我想在屏幕上显示它们,如{{}或使用ng bind。我试过了,但做不到 有人能帮我吗Angularjs 拦截器和ng绑定,angularjs,angular-http-interceptors,Angularjs,Angular Http Interceptors,我正在学习拦截器。我正在控制台上“打印”结果,但我想在屏幕上显示它们,如{{}或使用ng bind。我试过了,但做不到 有人能帮我吗 x.factory("inter", ["$q", function($q) { return { request: function(config) { console.log("Request: " + JSON.stringify(config)); return config; } }; }]); x.co
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 }}
非常棒的方法,我想知道如何将$scope发送到拦截器,但是没有任何意义。@JB,非常感谢!我还添加了响应,在我的例子中,用:{{ctrl.interceptedConfigs[$index]}}显示它们,你让我开心!