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?
$scope.contactlist
that is link to array instead of '$scope.contactsList()' that actually returns it?