1

I want a $http call in a custom filters that returns a value its not working(Asynch) and not returning a value.i want to do it by filters only.is it possible;

Here is my code:

  .filter('getSportName', ['$http', '$filter','BASE_URL','$rootScope','adapter',function ( $http, $filter,BASE_URL,$rootScope,adapter) {
    return function(id){
      var request = {
        method: 'get',
        command: 'SportsMasters/'+id,
      };
      adapter.call(request)
        .then(function (data) {
         var selectedSport = data;
          var name=data.sportstitle;
          return name;

        })
        .catch(function (err) {
          console.log(">>>>>>>error ",err);

        })

    }
  }])

Thanks.

0

1 Answer 1

1

I think you should not use filters that way. Filters are for transforming inputs based on optional params.

The problem here would be that you're immediately returning a promise from the filter function. And that's nothing Angular can deal with as a result from a filter.

My suggestion therefore would be this - fetch the result first, work with the filter based on the result:

As you'd be bombarding your server quite a bit with requests whenever you try to filter your data.

Sign up to request clarification or add additional context in comments.

2 Comments

i have more than 1000 nested records in my view .each record have a sport id.i want to get sport name on basis of sport id from $http call ...here only i can use a filter or a function call
You can nest $http calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.