I would like to do a request to my backend in a filter and return the result of my request. The problem is the service $http return a promise and it's the issue.
For present the issue I used a $timeout and the promises of angular in my fiddle : my fiddle
In my filter I use a $timeout with a promise but the final goal is to use a request http :
myApp.filter('filterHello', function ($http,$timeout,$q) {
return function (company_id) {
console.log("in the filter");
var deferred = $q.defer();
$timeout(function() {
deferred.resolve("ca marche");
}, 2000);
return deferred.promise;
};
});
Then in my view I use my filter who is suppose to display "ca marche" with a delay of 2 secondes but that doesn't work :
<div ng-controller="MyCtrl">
{{hello|filterHello}}
</div>
You can see that the filter return nothing and that there is an infinite loop in the filter because of the null promise I think.
If you don't understand why I want use a request http in a filter the answer is simple. For exemple I have an object user with the fields : email,name,company_id.. And I have an other object company with the fields : name, createOn,... I would like use the filter like this for display the name of the user's company :
{{user.company_id | ShowNameCompany}}
So, I need to do a request http in the filter to my company controller of my backend.
I hope someone can help me.