Reputation: 33
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?
Upvotes: 0
Views: 202
Reputation: 27232
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.
Upvotes: 1
Reputation: 835
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.
Upvotes: 1
Reputation: 15016
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.
Upvotes: 0
Reputation: 1822
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.
Upvotes: 0