Using Factory and Controller to handle Json

0

On this site Andrew McGivery I found this very interesting example:

.controller('UserCtrl', function($scope, $stateParams, userService) {
        var user = userService.getUser($scope.id);
    })
.factory('userService', function($http) {
    var users = [];

    return {
        getUsers: function(){
            return $http.get("https://www.yoursite.com/users").then(function(response){
                users = response;
                return users;
            });
        },
        getUser: function(id){
            for(i=0;i<users.length;i++){
                if(users[i].id == id){
                    return users[i];
                }
            }
            return null;
        }
    }
})

I would like to know how I would return all users if I had more than one user with the same id?

    
asked by anonymous 23.09.2015 / 15:12

1 answer

0

Only insert a list and at the end of for return the result, like this:

getUser: function(id){
    var users = [];
    for(i=0;i<users.length;i++){
        if(users[i].id == id){
            users.push(users[i]);
        }
    }
    return users;
}
    
12.05.2016 / 22:23