title: angular ionic - understanding models, factories and services
body: <p>i'm just starting to pick up ionic and angular and having trouble with understanding the implications of using factories/services for modeling.</p>
  
  <p>Scenario:  </p>
  
  <ul>
  <li>I want to model a Contact object that has attributes and methods.</li>
  <li>I want to an object AddressBook that has an attribute comprising a list of Contact objects and also has methods on it. </li>
  </ul>
  
  <p>from all the literature i've read, I want to create a factory/service for Contact and one for AddressBook.</p>
  
  <p>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?</p>
  
  <pre><code>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);
    */
  
  });
  </code></pre>
  
  <p>here's some of what i read already but still can't figure this out. </p>
  
  <p><a href="http://viralpatel.net/blogs/angularjs-service-factory-tutorial/" rel="nofollow noreferrer">http://viralpatel.net/blogs/angularjs-service-factory-tutorial/</a><br>
  <a href="http://mcgivery.com/ionic-using-factories-and-web-services-for-dynamic-data/" rel="nofollow noreferrer">http://mcgivery.com/ionic-using-factories-and-web-services-for-dynamic-data/</a><br>
  <a href="https://docs.angularjs.org/guide/providers" rel="nofollow noreferrer">https://docs.angularjs.org/guide/providers</a><br>
  <a href="https://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory">angular.service vs angular.factory</a>  </p>