1

I have two views and I use routes to access them. My main view is a list that uses ListController.

That controller does an $http request and gets some data back and on success I do: $scope.data = response.data;

When I click on a list item, my route goes to #/view/:id

I am able to get the id using routeParams in my other controller: DetailController

Since $scope.data was part of the ListController, how do I access say $scope.data in my DetailController?

(or at least pass $scope.data[some Id] to DetailController)

EDIT

app.factory('dataService', function(data)
{
  var data = data;
  return data;
});

the answer was to place this in my DetailController. response.data was still available to me

presentationService().then(
    function success(response)
    {
        log(response.data);
    },
    function error(response)
    {
        log(response);
    }
);
14
  • dataService should be making the $http request Commented Apr 1, 2013 at 23:01
  • @charlietfl see my last comment on InviS' post Commented Apr 1, 2013 at 23:02
  • here's another way that doesn't require using then in controller plunker.co/edit/o0opykZ7QOPfxGZCRNFp?p=preview Commented Apr 2, 2013 at 0:22
  • @charlietfl I like this method more than what I came up with. Since I am using ng-repeat on the $scope.presentations variable, the list isn't being populated. How can I tell when the request is finished from within the list controller? Commented Apr 2, 2013 at 17:18
  • shouldn't have to check if it's finished or not, listeners on scope object will modify DOM when data changes. Loop of pushing response into empty array is what's currently changing the data. Am trying to figure out another approach to updating data in service from $http using promises. Haven't got that solution figured out yet Commented Apr 2, 2013 at 17:33

1 Answer 1

2

You should not use scopes for sharing data between controllers. Use service instead. Watch this short movie: http://egghead.io/video/angularjs-sharing-data-between-controllers/

Hope it helps.

Sign up to request clarification or add additional context in comments.

6 Comments

ok I am trying to get this to work. In my http success function, I am doing function success(response) { dataService(response.data) } and I am getting dataService is undefined.` I'll post my factory in my question. I think I am doing it wrong. Also, I am passing dataService to my ListController
Move your $http tasks into service. And you will be able to cache data inside the service. And data will be able for sharing.
My http request is already a service. Let's make this easier...here is a pastie: pastie.org/7273427
Service that makes $http request should also share data between controllers.
I understand what you're saying, but I cannot get it to work. I am watching another youtube video on how this works
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.