0

I am using a filter to show some values in a table. Values fetching by ajax calls and I want to show a spinner while the values are fetching. So I wrote:

app.filter("fetchedValue", function(){
    return function(fetchedValue){
            if(angular.isUndefined(fetchedValue)){
                    return "image";//this is the spinner
            }else if(fetchedValue === null){
                    return "NA";
            }
            else{
                    return fetchedValue;
            }
    }
});

It is not working. I am not sure how to get the spinner when the value is undefined. Does anybody know how to do it?

dhrubo_moy
  • 1,144
  • 13
  • 31
Nisman
  • 1,271
  • 2
  • 26
  • 56

1 Answers1

1

I think you misunderstand what a filter is for. The function should return true or false depending if you want the value given to the function to be filtered or not.

See https://docs.angularjs.org/api/ng/filter/filter and AngularJS custom filter function

To answer your question though, forget about the filter, and just use an ng-if or an ng-show or an ng-hide on an <img> when the value is undefined.

<img ng-hide="fetchedValue" src="spinner.gif" />

This assumes that your fetched value is always truthy. A more robust solution would be to set a flag in a finally block of the request promise, and then ng-hide based on that flag.

Community
  • 1
  • 1
Seth Flowers
  • 8,990
  • 2
  • 29
  • 42
  • I think we can use filter to format the values in angular and this is what I am doing in that filter. – Nisman Sep 07 '16 at 20:20
  • Just because you can do something doesn't mean you should. The purpose of a filter in angular is not to modify the array, it is to select a subset of elements in the array. – Seth Flowers Sep 07 '16 at 20:22