title: AngularJS : Render controller without waiting factory variable to be intialized first
body: <p>First, I'm new to angularjs. I've create a factory to handle most of my data named &quot;store&quot;. Here is an example:</p>
  <pre><code>app.factory('store', function ($rootScope, $http, $q, api) {
  var data = {};
  
  return {
      
      setData: function () {
          var deferred = $q.defer();
  
          $http({
              method: 'GET',
              url: api.getData()
          }).then(function successCallback(response) {
              // handle data
              $rootScope.broadcast('store:data', data);
              deferred.resolve();
          }, function errorCallback(reponse) {
              // do stuff
              deferred.reject();
          });
      },
      
      getData: function () {
          return data;
      },
      
      addData: function (newData) {
          // do stuff
      },
      
      editData: function (newData) {
          // do stuff
      },
      
      deleteData: function (newData) {
          // do stuff
      }
      
    };
  });
  </code></pre>
  <p>I'm initializing this data inside my app.run function. BUT, I don't want my app to wait my data to be initialized first to render the controller. I want it to be rendered first and wait for updating when the data is initialized.</p>
  <pre><code>    store.setData()
      .then(function (response) {
          // do stuff
      })
      .catch(function (response) {
          // do stuff
      });
  </code></pre>
  <p>Here is how I'm getting the data updated inside my controller to be rendered</p>
  <pre><code>    $scope.data = store.getData();
  
      $rootScope.$on('store:data', function (event, data) {
          $scope.data = data;
      })
  </code></pre>
  <p>SO my problem is that I don't want to wait my data to be initialized to render my controller.
  Is there a solution to this problem ?</p>
  <p>Thanks a lot.</p>
  <p><strong>EDIT</strong> May 20 2021</p>
  <p>Btw if what I'm doing is wrong and there is better things to do, I'm open to any suggestions ! Thnx</p>
  <p><strong>EDIT</strong> June 9 2021</p>
  <p>Now I'm using $resource, but I don't know how can I get the new version of my list of data when I add new element to it.</p>
  <pre><code>agents: $resource(
          api.getAgents(),
          {},
          {
              get: {method: 'GET', isArray: false, cache: true},
              add: {method: 'POST', url: api.addAgent(), hasBody: true},
              edit: {method: 'PUT', url: api.editAgent(), params: {agentId: '@id'}, hasBody: true},
              delete: {method: 'DELETE', url: api.deleteAgent(), params: {agentId: '@id'}},
          }
      ),
  </code></pre>
  <p>Waiting for an answer. Thank you vm !</p>