(Note: This is AngularJs but should be a "standard" Javascript-Issue)
I use the following method to read the content of a json-file inside an angular controller:
$http.post(apiUrl + 'data.php?select', {directory: "customers", file: "john"})
.success(function (data) {
$scope.customers= data;
})
(The php - function simply returns a (valid) JSon-Array).
This works as expected; The content is stored inside $scope.customers
Because this kind of actions is needed in different places I decided to write a service for this:
myApp.factory('util', function ($http) {
return {
getFile:function(directory, file, target) {
$http.post(apiUrl + 'data.php?select', {directory: directory, file: file})
.success(function (data) {
target = data;
});
}
};
And then call this method the follwing way:
util.getFile("customers","john",$scope.customers);
But this does not work ($scope.customers remains empty).
After digging into SO I understand that this cannot work this way, because arrays are not passed by-reference but by-value.
But is there another way to achieve what I want to do?