0

I am reading values from json file. I created a sample json file.

contact.json:
  [
   {
     "id":"1",
     "Name":"abc"
   }
  ]

I created a service class

ContactsService.
    app.factory('ContactsService', function ($rootScope, $http, $log) {
        var contacts = [];
        return {
            getContactsList: function() {
                return contacts;
            },
            loadContactsFromJson: function () {
                var promise = $http.get('json/contacts.json')
                    .success(function(response) {
                        contacts = response;                    
                        return contacts;
                    })
                    .error(function(response) {                 
                        contacts = [];
                        return contacts;
                    });
                    return promise;
            }
        };
    }); 

In my controller class:

 init();
 function init() {       
   ContactsService.loadContactsFromJson();       
 }
 $scope.contactsList = function() {
   return ContactsService.getContactsList();
 };

but here contactsList is a function but I am trying to create an array contactlist in my controller class and trying to load the array in init() function. Later for the click event i want to add more contacts to this contactlist( contactlist.push). How do I read values from ContactsService.getContactsList() method to an array variable instead of function?

2
  • So you want to use $scope.contactlist that is link to array instead of '$scope.contactsList()' that actually returns it? Commented Nov 3, 2015 at 23:05
  • yes. if I am using $scope.contactList( later I cannot push objects in to it. I tried to assign in to an array $scope.contactlist = [] and in init method $scope.contactlist = ContactsService.getContactList(); but did not worked. Commented Nov 3, 2015 at 23:09

3 Answers 3

0

Create public variable (property of Service) and then just link Controller to it.

//Service
return {
    ...
    contactsList: []
    ...
}

and in Controller:

//Controller
$scope.contactsList = ContactsService.contactsList;
Sign up to request clarification or add additional context in comments.

1 Comment

Either just do contactsList: contacts for less refactoring.
0

I think you can let the promises you've set up do the work for you. Try something like this in the controller:

ContactsService.loadContactsFromJson().then(function (contacts) { 
  $scope.contactsList = contacts;
}

If you wanted to I suppose you could wrap that in your init function. Hope this helps!

Comments

0

In javascript objects are passed by reference. However when you do contacts = response inside a function copy of the reference is created and the initial reference is broken (call-by-reference), thus the changes made to the contacts variable are not applied to $scope.contactList.

Try doing smt. like contacts.push['test'] inside success function to see it yourself.

Comments

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.