Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : get back data from a json array with an id

Tags:

json

angularjs

I have a json file where i am stocking informations from all the people in my database. I actually use it to display first name, last name in a web page and i want to add the possibility to display the details of every person.

To do so i'm using the id of the person like this :

.when('/people/:id', {templateUrl: 'partials/people-detail.html'})

It works pretty well, i have a page generated for every person, which is nice. But now I would like to get the information of the person back.

The easiest way would have been to have a json file for every person, but i don't particularly like the idea of having so much file. So my actual idea is to iterate through the people.json file to find the good one and using it but it's not working.

Here's my controller :

var PeopleController = angular.module ('PeopleController', []);

PeopleController.controller('PeopleDetailCtrl', ['$scope', '$routeParams', '$http',
    function($scope, $routeParams, $http) {
        $scope.search = function() {
            var url = 'data/people.json';
            $http.get(url).success(httpSuccess).error(function() {
                alert('Unable to get back informations :( ');
            });
        }
        httpSuccess = function(response) {
            $scope.persons = response;
        }

        function getById(arr, id) {
            for (var d = 0, len = arr.length; d < len; d += 1) {
                if (arr[d].id === id) {
                    return arr[d];  
                }
            }
        }
        $scope.search();
        $scope.person = getById($scope.persons,$routeParams.id);
    }]);

Well, maybe my solution is bad since it doesn't work, but i didn't find another way to do so.

Now i'm all yours :)

Thanks for reading.

like image 263
Ryuu Avatar asked Dec 04 '25 05:12

Ryuu


1 Answers

The problem is that your $scope.search method contains $http.get() which is asynchronous.

What that means is your next line (the one that sets $scope.person) executes before the json file has been read. As such, $scope.persons is empty at the time it is executed.

You can take advantage of the fact that $http.get() returns a chainable promise here.

So if you change your search() function to return that promise, you can then use then() to populate person when everything has been successful:

$scope.search = function() {
  var url = 'data/people.json';

  return $http.get(url).success(httpSuccess).error(function() {
    alert('Unable to get back informations :( ');
  });

}

(note the return statement).

Then change the person population to take advantage of this:

$scope.search().then(function(){
  $scope.person = getById($scope.persons,$routeParams.id);
});
like image 123
Ed_ Avatar answered Dec 05 '25 22:12

Ed_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!