javascript - Angular text input filter initializes with no results -
i have basic angular app sorts , filters list of species local json file. when app initializes none of species returned. when begin type species returned, , if delete search terms species returned.
i species returned when app initializes.
i think it's neater use inline notation filter, avoids problem:
<div ng-repeat="animal in species | filter:query | orderby:orderprop")> note: can remove code sets $watch on query
edit: want in controller:
the reason filter initialising blank, because $scope.species data being populated asynchronously. when first $watch triggered, there no data filter. stays case until input query.
to solve this, set $watch after data has arrived, so:
$http.get('species.json').success(function(data) { $scope.species = data; $scope.$watch('query', function (query) { $scope.filtereddata = $filter('filter')($scope.species, query); }); }); alternatively manually run filter function once, inside success callback.
Comments
Post a Comment