ANSWERED by Fissio
I am trying to make a simple search feature in Angular. When the user types in something in the input field, I want to search through all the 'titles' in my JSON, and if a word matches with the input, then I want to bring back all the objects related to the match.
FACTORY
I first created a Factory to retrieve data from JSON using a Promise.
.factory('podcastData', function($http) {
var podcastData = {
async: function() {
var promise = $http.get('http://radio-sante-animale.fr/podcast-data.php').then(function(response) {
return response.data.categories;
})
return promise;
}
};
return podcastData;
})
Then in my controller, I tried to do a search program in my Controller. What I attempted so far, I managed to do a for loop, I got the length of the array, I then got all the 'Podcasts' in the array, and I then managed to get all the values matched with the input.
CONTROLLER
Updated
$scope.findValue = function(enteredValue) {
console.log(enteredValue);
var filtered = [];
podcastData.async().then(function(data) {
for (i = 0; i < data.length; i++) {
angular.forEach(data[i].podcasts, function(podcasts, key) {
var foundPodcasts = _.filter(podcasts, function(podcast) {
return podcasts.title.toLowerCase().indexOf(enteredValue) >= 0
});
if (typeof foundPodcasts !== 'undefined') {
filtered.concat(foundPodcasts);
console.log(foundPodcasts);
};
});
}
});
}
I updated my controller, now I do get in return all the objects that matches the search input, however when I console.log foundPodcasts, this is my response. This is good! However, there is empty arrays in between, and I am not able to do a NG-REPEAT, since there is no VALUE that I can grab.
Took the liberty of changing filtered
into array; I understood you wanted to find ALL the matching movies, whereas in your code you were just recreating the filtered
object on each match.
$scope.findValue = function(enteredValue) {
var filtered = [];
podcastData.async().then(function(data) {
for (i = 0; i < data.length; i++) {
var foundPodcasts = _.filter(data[i].podcasts, function(podcast) {
return podcast.title.toLowerCase().indexOf(enteredValue) >= 0
});
if (typeof foundPodcasts !== 'undefined') {
filtered = filtered.concat(foundPodcasts);
};
}
console.log(filtered);
return filtered;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With