0

I have a json file that has an array in it:

{
  "people": [
    {"number":"1", "name":"Gary", "surname":"Peterson", "age":"25"},
    {"number":"2", "name":"Andy", "surname":"Smith", "age":"26"},
    {"number":"3", "name":"Michael", "surname":"Johnson", "age":"28"}
    ]
}

I want to return only the first object (person record) to my application. When I call the http service in AngularJS and pass the parameter, like this:

angular.module('mod', [])
.controller('ctrl', function($scope, $http){

  $scope.serviceMethod = function(){
    $http.get("url to database.json", {params: {number: 1}})
    .then(function Succes(response) {$scope.person = response.data.people;});};      

});

it returns all three objects. I see no errors in the console in the console (status 200) and the parameter IS passed (it builds this url: http://urltowebsite.com/database.json?number=1).

What am I missing here?

2
  • 1
    How would angular know that it shouild filter on the number property?? Commented Sep 22, 2016 at 7:49
  • As i understand it is server side task nothing to do inside angular OR you can assign first element of returned JSON like $scope.person = response.data.people[0] Commented Sep 22, 2016 at 7:56

4 Answers 4

1

In your question you stated that you have a JSON file which contains records (I presume it is at server side) and you want this records to get filtered by this JSON file only then my answer would be that JSON file has no capability to filter its records.

for that you need server side application which reads this request

http://urltowebsite.com/database.json?number=1

and this server side application will access that JSON file, filter the records and return only specific record to AngularJS.

Then you do not need to any logic to filter records at AngularJS side.

Sign up to request clarification or add additional context in comments.

Comments

1

According to your requirement you should use server side language which filter the database records based on the number property and create a json object of the result.

I am using PHP for understanding the functionality :

Angular Code :

angular.module('mod', [])
.controller('ctrl', function($scope, $http){

  $scope.serviceMethod = function(){
    $http.get("getRecord.php", {number: 1})
    .then(function Succes(response) {$scope.person = response;});};      

}); 

getRecord.php :

<?php

$data = json_decode(file_get_contents("php://input"));
$number = $data->number;

$con = mysql_connect('localhost', 'root', '');
mysql_select_db('db_name', $con);

$query = 'select * from table_name WHERE number = 1';
$query_result = mysql_query($query);
$res = mysql_fetch_assoc($query_result);

do
{
  $resultdata[] = $res;
}while($res = mysql_fetch_assoc($query_result));
echo json_encode($resultdata);

mysql_close($con);
?>

Or if you want only first record from the JSON then you try this one.

$scope.person = response.data.people[0];

I hope it will work. Thanks.

Comments

0

You can't access to json file using parameter. You've fetched data from json correctly so now you can select on view site what you want to show.

Comments

0

If you want only the first person record, you could try something like this:

$scope.person = response.data.people[0];

If you wish to handle this on your server, you'll need your backend API to handle it and return only the first object of people array.

4 Comments

That is not an answer to the question. It is a quick fix.
Yeah, that would require some backend fixes.
Yes, but the point is not to return all the data every time I call the service. If it can't be done like this, then what is the usage of the parameter?
You'll have to handle the parameter on your backend API.

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.