Dynamic orderBy Breaks ng-repeat in view in AngularJS -
i having problem dynamically ordering data getting through service in controller. data loads fine when initial search, try update orderby, of data in views disappear instead of being reordered. data displaying in view data stored $scope.filtered_events. also, controller attached via router template.
search controller
(function () { .controller("search", function ($scope, zipcodesvc, $filter, $http) { $scope.$on("search", search); $scope.sortby = "date"; function search(evt, data) { $scope.zip_codes = []; $scope.locations = null; $scope.response = null; $scope.events = null; zipcodesvc.find(data.str, data.dist) .then( function (response) { $scope.locations = response; for(i=0; i<$scope.locations.zip_codes.length; i++){ $scope.zip_codes.push($scope.locations.zip_codes[i].zip_code); } console.log($scope.zip_codes); $http.get("api/events") .success(function(response){ $scope.events = response; //what want able dynamically order $scope.filtered_events = $filter('eventsinrange')($scope.events, $scope.zip_codes); }); }, function (err) { console.log("error finding cities: ", err); } ) } }) .filter("eventsinrange", function () { return function(events, zipcodes) { events_in_range = []; for(i=0;i < events.length; i++){ if(zipcodes.indexof(events[i].location.zip_code) > -1){ events_in_range.push(events[i]) } } return events_in_range; } }); }());
template
<div class="container"> <p class="bigger">filter:</p> <ul class="nav nav-tabs"> <li><a href='#' ng-click="sortby = 'type'">cya</a></li> <li><a href='#' ng-click="sortby = 'location.name'">location</a></li> <li><a href='#' ng-click="sortby = 'date'">date</a></li> <li><a href='#' ng-click="sortby = 'created_by'">creator</a></li> </ul> <div class="row"> <div ng-repeat="cya in filtered_events | filter:cyafilter | orderby:sortby" class="col-sm-4 text-center"> <h2 class="primary"> {{ cya.type }} </h2> <div class="well cyas"> <p> <span class="cya-label primary">creator:</span> {{cya.created_by}}</p> <img class="img-responsive cya-pic" src="http://fpoimg.com/150x150" alt="{{cya.location}} picture"> <div class="row"> <div class="col-sm-6"><p> <span class="cya-label primary text-left">date:</span><br> {{ cya.date }} </p></div> <div class="col-sm-6"><p> <span class="cya-label primary text-left">time:</span><br> {{ cya.time }} </p></div> </div> <p> <span class="cya-label primary">location:</span><br /> {{cya.location.name }} <br /> {{ cya.location.address }} <br /> {{ cya.location.city }}, {{ cya.location.state }} {{ cya.location.zip_code }}</p> <button class="btn btn-primary join" ng-show="false">join</button> </div> </div> </div> </div>
zip code service
(function () { angular.module("app.data") .factory("zipcodesvc", function ($http, $q) { return { find: findbylocation } function findbylocation(location, distance) { var url = "api/zipcodes?location=" + location + "&distance=" + distance; var defer = $q.defer(); $http.get(url) .success(function (response) { defer.resolve(response); }) .error(function (err) { defer.reject(err); console.log(err); }) return defer.promise; } }); }());
edit - added data structure of $scope.filtered_events
:
[ { date: "mm/dd/yyyy", location: { address: "someplace", city: "some city", zip_code: "12345", name: "some name", state: "some state" }, time: "hh:mm am", type: "some type", created_by: "some person" }, //etc ]
i suspect problem orderby
filter not updating $scope.filtered_events
binding. reason works correctly when add $filter('orderby')($scope.filtered_events, 'type')
directly in success
callback because $scope.$apply()
being called implicitly.
to fix add $scope.$apply()
@ end of orderby
function.
see this quick explanation.
Comments
Post a Comment