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?

ibsenv
  • 619
  • 1
  • 5
  • 18

3 Answers3

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/

Donal
  • 31,121
  • 10
  • 63
  • 72
  • As you suggested, I tried it with reloadOnSearch See this [fiddle](http://jsfiddle.net/HB7LU/13951/). But couldnt get it working so far. Is this even the current way to use reloadOnSearch? – ibsenv May 21 '15 at 21:00
  • @ibsenv unfortunately reloadOnSearch relates to search parameters (query strings). – Donal May 21 '15 at 21:28
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.

Community
  • 1
  • 1
ibsenv
  • 619
  • 1
  • 5
  • 18
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)

Community
  • 1
  • 1
Shehryar Abbasi
  • 378
  • 2
  • 8