2

My intent is to

  • stop reloading of the controller when using dynamic routes. eg: If I have a route defined as '/home/:param', and I am navigating from '/home/path1' to '/home/path2', the controller shoudnt get reloaded.

I tried to implement this by including templates using ng-include. Although it is not reloading the controller, the route url remains unchanged. Is there any other way to achieve this?

4
  • have you set reloadOnSearch on the route? Commented May 21, 2015 at 20:05
  • @Donal, I havent set reloadOnSearch, its using the default values. Commented May 21, 2015 at 20:10
  • ok, try setting it to false - it should not reload the controller then. Commented May 21, 2015 at 20:11
  • @Donal, pls see my comment on your answer. Commented May 21, 2015 at 21:02

3 Answers 3

2

Set reloadOnSearch to false on the route - to prevent the controller from reloading when a query parameter or hash in the url changes. For more info, see here.

reloadOnSearch only relates to query parameters or hashes. So you would have to use query parameters for it to work. For example, your links would be something like this:

 <a href="#/test?id=1">Link 1</a>
 <a href="#/test?id=2">Link 2</a>

You can pick up the id using the $location service, like this:

function testCtrl($scope, $location) {
    alert("reloading controller with id: " + $location.search().id);
}

There is an updated fiddle here: http://jsfiddle.net/donal/2cz4nz3o/1/

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

2 Comments

As you suggested, I tried it with reloadOnSearch See this fiddle. But couldnt get it working so far. Is this even the current way to use reloadOnSearch?
@ibsenv unfortunately reloadOnSearch relates to search parameters (query strings).
1

Picking up from Donal's answer, this is what I end up doing.

  1. Set reloadOnSearch=false in the routeprovider.
  2. Route Handing

    When getting routed via url, catch the param using $location.search() and load the corresponding template from within the controller.

    When loading a different template within the controller. trigger a function to change the template and update the url using $location.search('id',newTemplateId);Since the reloadOnSearch is set to false, it does noting but changes the url.

Have a look at this fiddle

Hope it helps somebody.

UPDATE : you may also want to set $routeUpdate when using reloadOnSearch. More info here.

Comments

0

here's an SO post for a similar question and some alternatives are given in the answers (alternatives to reloadOnSearch = false depending on your application)

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.