3

Using $routeProvider every time user clicks on a link, a new $scope is being generated. That means all the data is lost. How can i make Angular use the same controller/$scope?

Explanation:

http://jsfiddle.net/mpKBh/1/ (click on links)

<a href='#'>First controller</a>
<a href='#/view'>Second controller</a>

$routeProvider.
  when('/', { template:"{{$id}}",controller: ContentListCtrl}).
  when('/view', {template:"{{$id}}",controller: ContentDetailCtrl}).

P.s. is it possible to know which controller is currently active?

2 Answers 2

4

In AngularJS, $scope is not meant to hold data that persists across your application. For that, you want to use a service that is injected into both controllers. If you provide more detail on what data you're missing across routes, I would be happy to revise this answer to include something a little more actionable.

In re your PS: You can inject the $route service to get information about the current route; the $route.current.controller property will give you the constructor function of the current route.

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

5 Comments

This answer is correct. You could also use Angular's $cacheFactory if you have specific data that you would like to cache, but it is definitely correct that $scope gets destroyed when you change routes. If you don't want the $scope destroyed, and using services is not correct for your application, don't use routing and use includes instead.
I've implemented this jsfiddle.net/vojtajina/U7Bz9 example for infinite scroll, but when i'm changing routes, loadMore is still left binded, so if I trigger the controller 10 times i have 10 loadMore instances listening and at each scroll there will be 10 requests. So i was hoping that if i could persist the controller i wouldn't receive unnecessary requests. Any work arounds?
@n0mercy I'm not sure I follow why you would get so many listeners - or for that matter even more than one. Can you add some routing to this and use some console.log() statements to show what you mean?
This is what i was talking about: jsfiddle.net/xrYPb/1 i think the problem is that i'm using window.bind here. Am I right? jsfiddle.net/xrYPb this is the revision that always has 1 listener, but i need to bind to window scroll :/
Actually the question in a bit out of scope. Ill make a new thread with this concrete question. How to bind/unbind to 'window' in angular. Because i can't really find anything. Big thanks!
3

For those researching how to "unbind" in AngularJS, he is a bit of info (related to the OP's last comment above)

When a view is destroyed, it's basically marked for garbage collection - but it's still there. That is why you are getting multiple requests when a scroll happens - because it's still listening for events.

So the easiest way to deal with this (that I have found, though I'd like to learn of other ways) is to listen for the $destroy event and react on it.

You can "unbind/unlisten" for an event by keeping a reference to what is returned by the $on method. Here is an example taken from a controller:

$scope.systemListener = $rootScope.$on("someEventYouListenTo", function (event, data) {
  console.log('Event received by ' + $scope.name);
});

$scope.$on('$destroy', function () {
    // Remove the listener
    $scope.systemListener();
});

Now those old scopes/views won't react to events anymore.

Hope that helps someone!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.