0

Let's say I have an entity called "Entity" that has three properties - "Id", "Name" and "Number".

Using AngularJS, how can I use $http.get to send a GET request for only the objects with a specific "Number"? For example, out of all the objects in that entity, I only want to GET the objects with the number 20. How can I do that?

Thank you!

3
  • Where are the data from ? an api ,? It depend your storage structure Commented Aug 11, 2016 at 9:17
  • Yes, sorry. It's from a Web API in Visual Studio. Commented Aug 11, 2016 at 9:18
  • Is this what you are looking for? stackoverflow.com/questions/13760070/… Commented Aug 11, 2016 at 9:21

2 Answers 2

1

It depends your API but you could do something like

     $http({
         method  : 'GET',
         url     : 'your_api_path/object/20',
         headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })
     .success(function(data) {
         $scope.data = data
      });

or

$http({
    url: 'your_api_path', 
    method: "GET",
    params: {object_id: object.id}
 });
Sign up to request clarification or add additional context in comments.

Comments

1

You must pass an criteria on which your API will return the corresponding value.

You can have an class

[Route("api/[controller]")]
public class EntityController : Controller
{

   [HttpGet("{number}")]
   public Entity Get(int number)
   {
       return yourList.Where(en => en.Number == number).FirstOrDefault();
   }
}

and an $http request

$http.get('yourHost://api/entity/20')
     .success(function(data) {
         $scope.data = data
      });

or

$http({
    url: 'yourHost://api/entity', 
    method: "GET",
    params: {number: yourNumber}
 });

7 Comments

I'm having trouble understanding the changes you want me to make on my Visual Studio controller. My controller is the default one, as I didn't make any changes to it. Do I put your code in the GET: api/Entity section? Can you also explain your variables a bit more, like the yourList and the number? Is that number the actual number I want to filter from the "Number" property, i.e. 20?
@T.Ferreira don't change your controller, only adjust it. I only show you an example. number is the parameter you pass, yourList is the list of items from which you want to get the values
@T.Ferreira I only say that you need to pass the number on which you want to filter to the API method, and your API must return the properly value
Please check this pastebin, it is the part of my controller that relates to GET: pastebin.com/gNYEjtd5. ITApps_KBTM_Execution is the name of the entity. What changes should I make here in order to filter the parameter "Number"?
@T.Ferreira create an method which will take an number an your list will find which one's number is equal to the passed number and return that item
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.