title: Not able to lazily update a controller scope with a REST service - Angularjs
body: <p>I have basically created a cache in my <code>Angular</code> service.  If the <code>REST</code> call has already been made, the an array is returned, else it makes the <code>REST</code> call to get the needed info.  Here is my service:</p>
  
  <pre><code>define(function(require) {
  
  var module = require('portal/js/services/app.services');
  
  return module.factory('providerService', function($resource) {
    return {
      requestingProviders: [],
      getRequestingProviders: function() {
        var that = this;
        if(this.requestingProviders.length === 0) {
          this.getResource().search({
            role: 'REQUESTING_PROVIDER'
          }, function(data) {
            that.requestingProviders = data.providers;
            return that.requestingProviders;
          });
        } else {
          return that.requestingProviders;
        }
      },
      getResource: function() {
        return $resource('/api/v1/providers/:providerId', {providerId:'@id'}, {
          search: {
            method: 'GET',
            headers: {
              'RemoteUser': 'jhornsby',
              'Content-Type': 'application/json'
            },
            params: {
              limit: 2000,
              organizationId : '0001194'
            }
          }
        });
      }
    };
  };
  </code></pre>
  
  <p>Here is an example of me accessing the service from a controller:</p>
  
  <pre><code>define(function(require) {
    var module = require('portal/js/controllers/app.controller');
  
    module.controller('AddPatientCtrl', function($scope, providerService) {
     $scope.providers = providerService.getRequestingProviders();
    };
  
    return module;
  });
  </code></pre>
  
  <p>When the <code>REST</code> call returns, it does not update my <code>scope.providers</code> variable in my <code>controller</code>.  When I try to access it a 2nd time, then it works fine by accessing the array instead of calling out to the <code>REST</code> service.  WHy won't my <code>scope</code> update in my <code>controller</code> during the <code>REST</code> call?  </p>