javascript - Filter json data in controller, the right way -
(there other questions neither helped me out)
hi, know if right way filter results service i'm displaying 1 result (like detail).
i know use ng-repeat , filter in view , cleanest, want have more control on because re-use of data in controller other operations.
right i'm doing this:
$scope.savedevents = event.getpayedevents(); //gets list service //goes through entire list , checks match angular.foreach($scope.savedevents, function(event) { if (event.idevent == $stateparams.eventid) { $scope.showevent = event; } }); //now if there match can use $scope.showevent.eventname etc
not sure if easier using $filter return 1 event has correct idevent. or if has better solution, please let me know.
thanks
i don't see problems have, inject $filter
service , 1 liner:
$scope.showevent = $filter('filter')($scope.savedevents, { idevent: $stateparams.eventid });
edit: here easy way resolve result single value returned array:
var showevents = $filter('filter')($scope.savedevents, { idevent: $stateparams.eventid }); $scope.showevent = showevents && showevents.length ? showevents[0] : null;
in coffeescript little more concise:
$scope.showevent = $filter('filter')($scope.savedevents, { idevent: $stateparams.eventid })?[0]
Comments
Post a Comment