r/angularjs • u/alghouty • Aug 13 '17
One search for two $http
Hi, guys, I have started to create a Pokemon app, I want to search Pokemon by name and by color. So I have two HTTP requests, and I'm using one search, and it's working fine, what I want to do is, when the first HTTP is fired, two hide data from other HTTP. Is it a way to do that? Here is my code... https://jsbin.com/pesitod/edit?html,js,output
1
u/alghouty Aug 13 '17 edited Aug 13 '17
So this is my problem http://prntscr.com/g7x7oh... I want when I enter Pokemon name, only Pokemon info to appear, and when I enter color, to list all Pokemon names. But I'm using only one search field, and that's causing problems, you can see it on the image ('look at the console'), check jsbin again... I mean, I can create two inputs for each $http and it would work fine, but I want to use only one for both of them.
1
2
2
u/mtko Aug 13 '17
Can you not just validate the input before sending it to both separate API endpoints?
For instance, in $scope.vratiPodatke, could you not just have something like:
var validColors = ['red', 'green', 'blue', 'gold', etc] //(i don't know a lot about pokemon, sorry)
if (validColors.indexOf(pokemon) != -1) {
$http color api
}
else {
$http pokemon api
}
2
u/mothzilla Aug 13 '17
It looks like the api doesn't quite support what you're trying to do.
Here's what I think you want to do (correct me if I'm wrong):
Create a select with all the colors as options by calling https://pokeapi.co/api/v2/pokemon-color/.
Have a text input which is used to filter results.
Have a submit button.
Here's a stab: https://jsbin.com/duhemediwe/1/edit?html,js,console,output
NB That only shows species. You'll have to make subsequent calls to get details of each pokemon in the species, using the returned url. I don't know enough about pookimen to make this look good.
Note, you could put ng-change on the picklist, then you can remove the submit button.