0

My question is clear i guess, i have the bellow code in my controller

vf.filtroses = [];
    FiltrosService.currentuser().success(function(data, status) {
        vf.filtroses = data;
    });

i want to verify if vf.filtroses has elements or it's empty. i tryed .lenght and equals:

vf.nofiltros = false;
    vf.filtroses = [];
    FiltrosService.currentuser().success(function(data, status) {
        vf.filtroses = data;
    });
    if(angular.equals([], vf.filtroses)){
        vf.nofiltros = true;
    }

but it does not work for me !

any ideas plz??

Liam
  • 27,717
  • 28
  • 128
  • 190
Smahane
  • 175
  • 1
  • 5
  • 13

3 Answers3

1

Put the size check inside the success function:

FiltrosService.currentuser().success(function(data, status) {
    vf.filtroses = data;
    if(!(vf.filtroses && vf.filtroses.length>0)){
             vf.nofiltros = true;
             }
    });
Sreehari S
  • 388
  • 1
  • 12
0

You can use .length to check the length of the array or list of objects.

vf.nofiltros = false;
vf.filtroses = [];
FiltrosService.currentuser().success(function(data, status) {
    vf.filtroses = data;

    vf.nofiltros = vf.filtroses && vf.filtroses.length > 0;
});
Alok
  • 1,290
  • 1
  • 11
  • 21
-1

Check null or undefined will solve your problem.

if(vf.filtroses!==undefined && vf.filtroses!=null){
    vf.nofiltros = true;
}
Arun Shinde
  • 1,185
  • 6
  • 12