Using Angular Service to pass information between views with the same controller
<p>I have a multistep form accross several views that share a controller.  Here are the routes:</p>

<pre><code>.state('tab.newEventCategory', {
  url: '/activities/new-event-category',
  views: {
    'tab-patient': {
      templateUrl: 'templates/new-event-category.html',
      controller: 'ActivityDashboardCtrl'
    }
  }
})
.state('tab.newEventSubCategory', {
  url: '/activities/new-event-sub-category',
  views: {
    'tab-patient': {
      templateUrl: 'templates/new-event-sub-category.html',
                controller: 'ActivityDashboardCtrl'
    }
  }
})
.state('tab.selectEventAttendees', {
  url: '/activities/select-event-attendees',
  views: {
    'tab-patient': {
      templateUrl: 'templates/select-event-attendees.html',
      controller: 'ActivityDashboardCtrl'
    }
  }
})
.state('tab.addPictures', {
  url: '/activities/add-pictures',
  views: {
    'tab-patient': {
      templateUrl: 'templates/add-pictures.html',
      controller: 'ActivityDashboardCtrl'
    }
  }
})
</code></pre>

<p>I am trying to make it so that as a user goes through the form his answers will be saved to a variable called <code>formData</code>. Here is the controller:</p>

<pre><code>.controller('ActivityDashboardCtrl', function($scope, $state, EventCategory, EventForm, EventSubCategory, Patient, $cordovaImagePicker, $ionicPlatform, CreateEvent) {

  $scope.formData = CreateEvent;
  $scope.event_categories = EventCategory.query();
  $scope.event_sub_categories = EventSubCategory.query();
  $scope.patients = Patient.query({facility_user_id: 1});

  $scope.moveToEventSubCategory = function(event_category){
    $scope.formData.category = event_category;
    $state.go('tab.newEventSubCategory');
  }
  $scope.createSubCategory = function(sub_categories){
    $state.go('tab.selectEventAttendees');
  }

  $scope.addAttendeesToFormData = function(event_attendees){
    $state.go('tab.addPictures');
  }

  $scope.saveForm = function(){
      $scope.formData.$save();
  }

});
</code></pre>

<p>I am new to Angular and I am finding it somewhat difficult to understand how services work.  </p>

<p>As you can see in the code above, at the first part of the form, $scope.formData is set to the CreateEvent service:</p>

<pre><code>.factory('CreateEvent', function($resource){
  return $resource("http://localhost:3000/api/events");
})
</code></pre>

<p>Here is the first page of the form.  When this part is submitted, the <code>moveToEventSubCategory</code> function is called:</p>

<pre><code>&lt;ion-view view-title="Activity Dashboard"&gt;
  &lt;ion-content&gt;

    &lt;div ng-repeat="event_category in event_categories" class="padding"&gt;
        &lt;a class="button button-block button-positive button-large" ng-click="moveToEventSubCategory(event_category)"&gt;
          {{event_category}}
        &lt;/a&gt;
    &lt;/div&gt;
  &lt;/ion-content&gt;
&lt;/ion-view&gt;
</code></pre>

<p>This is succesfully passing information to the next page of the form because the expressions on that page are evaluating properly with formData.  On the second page, the user picks subcategories:</p>

<pre><code>&lt;!-- client/app/views/main.html --&gt;
&lt;ion-view view-title="New Event Subcategory"&gt;
    &lt;ion-content class="padding"&gt;
    formData Category:
    {{formData.category}}
    &lt;form ng-submit="createSubCategory(formData)"&gt;
        &lt;div ng-repeat="sub_category in event_sub_categories"&gt;
            &lt;ion-checkbox ng-model="formData.subcategories[sub_category]" ng-true-value="'{{sub_category}}'"&gt;
              {{sub_category}}&lt;/ion-checkbox&gt;
        &lt;/div&gt;
        &lt;button type="submit" class="button button-block button-positive"&gt;Continue&lt;/button&gt;
    &lt;/form&gt;
    &lt;/ion-content&gt;
&lt;/ion-view&gt;
</code></pre>

<p>On the final page, I want to send the information to the server, so I have:</p>

<pre><code>&lt;button class="button button-full button-positive" ng-click="saveForm()"&gt;
    Submit
&lt;/button&gt;
</code></pre>

<p>When clicking on the button, I get the following error in my browser console:</p>

<pre><code>ionic.bundle.js:25642 TypeError: $scope.formData.$save is not a function
    at Scope.$scope.saveForm (controllers.js:54)
    at fn (eval at &lt;anonymous&gt; (ionic.bundle.js:26457), &lt;anonymous&gt;:4:215)
    at ionic.bundle.js:62386
    at Scope.$eval (ionic.bundle.js:29158)
    at Scope.$apply (ionic.bundle.js:29257)
    at HTMLButtonElement.&lt;anonymous&gt; (ionic.bundle.js:62385)
    at HTMLButtonElement.eventHandler (ionic.bundle.js:16583)
    at triggerMouseEvent (ionic.bundle.js:2948)
    at tapClick (ionic.bundle.js:2937)
    at HTMLDocument.tapMouseUp (ionic.bundle.js:3013)(anonymous function) @ ionic.bundle.js:25642(anonymous function) @ ionic.bundle.js:22421Scope.$apply @ ionic.bundle.js:29259(anonymous function) @ ionic.bundle.js:62385eventHandler @ ionic.bundle.js:16583triggerMouseEvent @ ionic.bundle.js:2948tapClick @ ionic.bundle.js:2937tapMouseUp @ ionic.bundle.js:3013
</code></pre>

<p>Can someone please give me a good explanation as to how services work to pass information between views and also specifically how to fix the error I'm getting?</p>

<p>UPDATE:</p>

<p>Based on some feedback, I've rewritten the service to this:</p>

<pre><code>.factory('CreateEvent', function($resource){
  // var eventFormData = eventFormData || {};
  // return $resource("http://localhost:3000/api/events");
          var formData = {};

          return {
            getFormData:function(){
                     return formData;
            },
            setCategory:function(category){
                  formData.category = category;
            },

            setSubCategories:function(sub_categories){
              formData.sub_categories = sub_categories;
            },

            createEvent:function(){
              return $resource("http://localhost:3000/api/events");
            }
        } 
})
</code></pre>

<p>Then to save object with an api call i do <code>$scope.formData.createEvent().$save();</code> but I am still receiving the same error.</p>