29

I started developing in AngularJS. I'm confused as to whether this is a proper design to pass data between my partial views.

Right now I have a loader page where I do some request.

function PeopleController($scope,$http,$location){
    $http.get('/location/-79.18925/43.77596').
    success(function(data){
      $scope.savePeopleResponse(data);
      $location.url('/test');
    });
}

Then in the view that gets loaded for /test

I am just calling

<div ng-controller="resultController">
    <div class="blueitem">{{getResultForPeople()|json}}</div>
</div>

[resultController]

    function resultController($scope){
      $scope.getResultForPeople = function(){
     return $scope.getPeopleResponse();
    }
}

and the savePeopleResponse and getResultForPeople are "cached" in the rootScope as such

app.run(function($rootScope) {
  var peopleResponse = {};
  $rootScope.savePeopleResponse = function(data) {
   peopleResponse = data;
   console.log(data);
  }

  $rootScope.getPeopleResponse = function(){
    return peopleResponse;
  }
});

Now as you can see, this will get very messy if this application grows larger and larger. What's the best way to handle data so that it's persisted across controllers?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Faisal Abid
  • 8,900
  • 14
  • 59
  • 91

1 Answers1

55

You can persist data across controllers by creating your own service as described nicely in this blog. You can also refer to this question.

In your case you can move your savePeopleResponse and getPeopleResponse into a service and then inject the service into any controllers you would like to access it.

angular.module('myApp', [])
    .factory('peopleService', function () {
        var peopleResponse = {};

        return {
            savePeopleResponse:function (data) {
                peopleResponse = data;
                console.log(data);
            },
            getPeopleResponse:function () {
                return peopleResponse;
            }
        };
    });

With your controller something like this:

function resultController ($scope, peopleService) {
    $scope.getResultForPeople = peopleService.getPeopleResponse;
}

With this code example make sure you include ng-app="myApp"

Community
  • 1
  • 1
Gloopy
  • 37,767
  • 15
  • 103
  • 71
  • 1
    Ah I see, With services though, aren't I pretty much doing the same thing as I did with $rootScope. Except that its a service. Whats the BIG difference? I would have access to rootScope everywhere also. – Faisal Abid Sep 25 '12 at 00:51
  • 14
    It is similar but with Angular's dependency injection it makes it easy to choose the services you want to use in each controller without cluttering the rootScope (promotes separation of concerns). You can also inject mock services for unit testing your controllers which can make testing easier. You can also move your $http access into the service so the get/save is all in one place and not across multiple controllers. – Gloopy Sep 25 '12 at 01:04
  • Oh okay that makes a lot of sense. Thanks much more clearer now. – Faisal Abid Sep 25 '12 at 01:12
  • 3
    Why the decision to use a service instead of a factory? Isn't a service meant to work as an initialiser to objects? Here it's being used in a module pattern. I'm new to Angular so may be understanding it incorrectly. – leepowell Jul 04 '13 at 20:52
  • 1
    You're right I would use a `factory` instead of `service` in this case. Others can take a look at [this post](http://stackoverflow.com/questions/15666048/angular-js-service-vs-provide-vs-factory/15666049#15666049) and [this post](http://stackoverflow.com/questions/13762228/confused-about-service-vs-factory/13763886#13763886) for more info on a service vs factory. The app ends up behaving the same as far as I can tell despite the nuances. – Gloopy Jul 07 '13 at 05:36
  • Ive been following this pattern of inter controller / view communication. But im ending up with a lot of these Services that are basicly just global vars for sharing between views and which im manually managing in terms of clearing out when data is abd etc etc. is this still the best option? – Chris Matheson Feb 12 '14 at 16:51
  • I've still been using services for this purpose and haven't found a better option though I haven't looked too hard for one. I'd be interested if any come up. – Gloopy Feb 12 '14 at 23:50
  • Shouldn't this be called peopleFactory instead of peopleService? – Northstrider Feb 16 '15 at 08:01
  • Good explanation in the blog – marcostrama Jun 10 '15 at 21:02