i'm just starting to pick up ionic and angular and having trouble with understanding the implications of using factories/services for modeling.
Scenario:
- I want to model a Contact object that has attributes and methods.
- I want to an object AddressBook that has an attribute comprising a list of Contact objects and also has methods on it.
from all the literature i've read, I want to create a factory/service for Contact and one for AddressBook.
But since Contact is a singleton, how do I put together an array of these things? once i "create" a new contact, because it is a singleton, doesn't it just make changes on the original?
angular.module('blah.services', ['ionic.utils'])
.factory('Contact', function($q){
var o = {
"name" : '',
"email" : ''
};
return o;
})
.factory('AddressBook', function($q, Contact){
var o = {};
o.contacts = [];
o.addContact = function(contact){
o.contacts.push(contact);
}
return o;
});
// and then somehwere something like
angular.module('blah.controllers', ['blah.services'])
.controller('somethingCtrl', function($scope, Contact, AddressBook) {
/* what am i supposed to do here?
is it something like
var c = new Contact();
c.name = 'foo';
AddressBook.addContact(c);
*/
});
here's some of what i read already but still can't figure this out.
http://viralpatel.net/blogs/angularjs-service-factory-tutorial/
http://mcgivery.com/ionic-using-factories-and-web-services-for-dynamic-data/
https://docs.angularjs.org/guide/providers
angular.service vs angular.factory