[英]Angular filter with $http response
I need a filter where i need to make $http call and then return that response . 我需要在需要进行$ http调用然后返回该响应的过滤器。 i am trying to use angular promise but nothing works . 我正在尝试使用有角度的承诺,但没有任何效果。 Return is not waiting for the response . 返回不等待响应。 Take a look below for my code. 在下面查看我的代码。 It is returning {} , Not waiting for $http response. 返回{},不等待$ http响应。 I need this using filter only. 我只需要使用过滤器。 Idea is to keep it separate from Controller , so it can be used anywhere later. 想法是将其与Controller分开,以便以后可以在任何地方使用。
.filter('filterXML',['testOne','$sce',function(testOne, $sce){
return function(url){
return testOne.getTest(url).then(function(data){
return data;
});
}
}])
.factory('testOne', ['$http', function($http){
return {
getTest: function(url){
return $http.get(url).success(function(data){
console.log(data)
return data;
})
}
}
}])
Goal : {{ ' http://rss.cnn.com/rss/cnn_topstories.rss ' | 目标:{{' http://rss.cnn.com/rss/cnn_topstories.rss '| filterXML}} filterXML}}
So it will return all data from this rss feed . 因此它将返回此rss feed中的所有数据。
I don't want to use controller . 我不想使用controller 。 Idea is to make separate module and call it anywhere in application . 想法是制作单独的模块并在应用程序中的任何位置调用它。
Any help will be appreciated . 任何帮助将不胜感激 。 Thanks 谢谢
You shouldn't be making http calls in a filter that's just not what filters are for. 您不应该在不是用于过滤器的过滤器中进行http调用。 Filters are usually passed an array of objects and only the objects that pass a certain condition will be returned in the new filtered array. 过滤器通常通过对象数组传递,只有通过特定条件的对象才会在新的过滤后数组中返回。
I think you want to go for a set up more like this... 我想您想进行这样的设置...
angular.module('foo', [])
.controller('someController', ['$scope', '$filter', 'testOne', function($scope, $filter, testOne){
testOne.getTest('foo/bar').then(function(data){
$scope.myFilteredData = $filter('filterXML')(data);
});
}])
.filter('filterXML', function() {
return function(input) {
// the condition on which to filter the input goes here...
// this is just dummy code you will have to work out your own logic
return (input === 'XML');
};
});
.factory('testOne', ['$http', function($http){
return {
getTest: function(url){
return $http.get(url)
.success(function(data){
console.log(data)
return data;
});
}
}
}])
Use a service. 使用服务。 In fact you've already created one: 实际上,您已经创建了一个:
.factory('testOne', ['$http', function($http){
return {
getTest: function(url){
return $http.get(url).success(function(data){
console.log(data)
return data;
})
}
}
}])
Now just inject $testOne into your controller and then in your controller do: 现在只需将$ testOne注入您的控制器,然后在您的控制器中执行以下操作:
var service = $testOne;
service.getTest('http:/some.url').then(function(response){ /*do something with the response */});
I don't want to use controller . 我不想使用controller。 Idea is to make separate module and call it anywhere in application . 想法是制作单独的模块并在应用程序中的任何位置调用它。
See i want to keep my code separate from Controller . 看到我想使我的代码与Controller分开。 Otherwise everytime i have to put these line in each controller .So calling it from a filter and then it can run where ever we will call . 否则每次我都必须将这些行放在每个控制器中。因此从过滤器中调用它,然后它就可以在我们将调用的任何地方运行。
I suggest you turn it into a service, and then (to be able to use it anywhere ), expose the methods of said service that you want to utilize onto $rootScope
* (because let's face it, a filter
is not what you are looking for). 我建议您将其转换为服务,然后(以便可以在任何地方使用),将要使用的所述服务的方法公开到$rootScope
*(因为面对现实, filter
不是您想要的东西)对于)。
app.service('someServiceName', function ($http) {
function getStuff (url) {
return $http.get(url).then(function (res) {
return res;
});
}
angular.extend(this, {
getStuff: getStuff
});
});
app.run(function ($rootScope, someServiceName) {
$rootScope.getStuff = someServiceName.getStuff;
});
{{ getStuff(url) }}
*: I don't usually recommend exposing stuff onto $root
, but you seem pretty dead set on not using DI (if I'm reading all of the comments/answers/question(s) correctly) into the $scopes
where this will be presented. *:我通常不建议将内容公开到$root
,但是如果不使用DI(如果我正确读取所有评论/答案/问题),您似乎已经死定了,这将在$scopes
中进行被呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.