proper way to call $filter in the controller?

188 views
Skip to first unread message

yahya Kacem

unread,
Mar 8, 2013, 9:04:39 AM3/8/13
to ang...@googlegroups.com
Hi, I have this forked code plunker for pagination it was working fine but after I made some adjustment to make it grub the data from a Json file it stop working.
To see code working comment lines: [13, 14, 15], then uncomment lines: [16 -> 37].
The error is about a problem with the $filter function it returns undefined when the data are fetched from a Json file so I think because the object is a promise now that breaks the $filter function, I'm not sure.
Any help thanks in advance.

yahya Kacem

unread,
Mar 8, 2013, 9:33:00 AM3/8/13
to ang...@googlegroups.com
Error message:

TypeError: Cannot read property 'length' of undefined
          at Object.$scope.groupToPages (controllers.js:66:45)
          at Object.$scope.search (controllers.js:61:12)
          at new controllers.MainCtrl (controllers.js:99:10)


Sander Elias

unread,
Mar 8, 2013, 9:41:52 AM3/8/13
to ang...@googlegroups.com
Hi Yahya,

You plunker seems incomplete. You are loading a file items.json that is not attached to this plunker. Also on app.js line 10, you are setting directives to an undefined variable.
If you fix this, I might take another look.

With kind regards
Sander Elias

On Friday, March 8, 2013 3:04:39 PM UTC+1, yahya Kacem wrote:

yahya Kacem

unread,
Mar 8, 2013, 9:51:02 AM3/8/13
to ang...@googlegroups.com
Sorry, about that, it working now, the same plunker.


On Friday, March 8, 2013 3:04:39 PM UTC+1, yahya Kacem wrote:

Sander Elias

unread,
Mar 8, 2013, 10:15:27 AM3/8/13
to ang...@googlegroups.com
Sorry, have to leave now.
took a quick peek, and indeed there is something fishy with your filter.
If you can wait, I will take a longer look tomorrow!

Regards
Sander Elias

yahya Kacem

unread,
Mar 8, 2013, 1:19:54 PM3/8/13
to ang...@googlegroups.com
OK, thanks.

On Friday, March 8, 2013 3:04:39 PM UTC+1, yahya Kacem wrote:

Florian Orben

unread,
Mar 8, 2013, 1:57:47 PM3/8/13
to ang...@googlegroups.com
Hey there,

actually your usage of filter is correct - to make it working there are two solutions (depending on how you want it):
Wait until the promise is resolved before calling $scope.search, i.e.

Items.then(function(data) {
  $scope.items = data;
  
  //call $scope.search here, instead of calling it on line 106
  $scope.search();
});
this seems to be the better solution in my opionion.

The other way to do it, would be to initialize $scope.items with an empty array, i.e. at the top of your controller declare:
$scope.items = [];
The problem right now is, that the filters are running before $scope.items is set ($scope.items is therefore undefined, and has no .length attribute), 
by previously assigning it an empty array, this bug would be fixed.

- Flo

yahya Kacem

unread,
Mar 8, 2013, 3:02:35 PM3/8/13
to ang...@googlegroups.com
+Florian Orben Thanks that did it.


On Friday, March 8, 2013 3:04:39 PM UTC+1, yahya Kacem wrote:
Reply all
Reply to author
Forward
0 new messages