Updating URL in Angular JS without re-rendering view
Asked Answered
S

9

80

I'm building a dashboard system in AngularJS and I'm running into an issue with setting the url via $location.path

In our dashboard, we have a bunch of widgets. Each shows a larger maximized view when you click on it. We are trying to setup deep linking to allow users to link to a dashboard with a widget maximized.

Currently, we have 2 routes that look like /dashboard/:dashboardId and /dashboard/:dashboardId/:maximizedWidgetId

When a user maximizes a widget, we update the url using $location.path, but this is causing the view to re-render. Since we have all of the data, we don't want to reload the whole view, we just want to update the URL. Is there a way to set the url without causing the view to re-render?

HTML5Mode is set to true.

Scintilla answered 20/8, 2013 at 14:3 Comment(1)
If you want your data to survive route changes on the client side, why not push it into a service?Algae
N
133

In fact, a view will be rendered everytime you change a url. Thats how $routeProvider works in Angular but you can pass maximizeWidgetId as a querystring which does not re-render a view.

App.config(function($routeProvider) {
  $routeProvider.when('/dashboard/:dashboardId', {reloadOnSearch: false});
});

When you click a widget to maximize:

<a href="#/dashboard/1?maximizeWidgetId=1">Maximum This Widget</a>
or
$location.search('maximizeWidgetId', 1);

The URL in addressbar would change to http://app.com/dashboard/1?maximizeWidgetId=1

You can even watch when search changes in the URL (from one widget to another)

$scope.$on('$routeUpdate', function(scope, next, current) {
   // Minimize the current widget and maximize the new one
});
Neotropical answered 31/8, 2013 at 18:32 Comment(6)
If it will be accepted answer I will create separate question without any ":param" things. Just plain stupid question: how to not re-render view.Seyler
I think angular-ui-router will be an obvious choice in that case.Neotropical
I'm using the $routeUpdate event. I think the $routeChangeStart gets triggered when moving to another location.Kurbash
@kuchumovn: Unfortunately we are still in 2014. Angular 2.0 will have a revamped routing (probably angular-ui-router built-in) which would do what you intended.Neotropical
@codef0rmer, I have found this answer more than once. Thanks! Great answer, still after all this time!Musset
Adding question mark programatically will cause the URL to have a %3F instead, so I suggest using: $location.path('/store/items').search({'maximizeWidgetId', 1}) Though, this still did not work for me, so angular-ui-router, here I come.Mopes
W
9

You can set the reloadOnSearch property of $routeProvider to false.

Possible duplicate question : Can you change a path without reloading the controller in AngularJS?

Regards

Winded answered 20/8, 2013 at 14:44 Comment(3)
That looks like it is a duplicate. Unfortunately it looks like they have the same issue, as long as it's part of the url path, not a query variable, it refreshes.Scintilla
No, it's not duplicate.Seyler
It's not a duplicate, since the linked question is about not reloading controller and this is about not reloading the template. The solution in the linked question will make the view reload.Mopes
A
6

For those who need change full path() without controllers reload

Here is plugin: https://github.com/anglibs/angular-location-update

Usage:

$location.update_path('/notes/1');
Alfonso answered 12/8, 2015 at 11:36 Comment(1)
thanks, works. But maybe you shouldn't extend original $location?Unmerciful
O
4

I realize this is an old question, but since it took me a good day and a half to find the answer, so here goes.

You do not need to convert your path into query strings if you use angular-ui-router.

Currently, due to what may be considered as a bug, setting reloadOnSearch: false on a state will result in being able to change the route without reloading the view. The GitHub user lmessinger was even kind enough to provide a demo of it. You can find the link from his comment linked above.

Basically all you need to do is:

  1. Use ui-router instead of ngRoute
  2. In your states, declare the ones you wish with reloadOnSearch: false

In my app, I have an category listing view, from which you can get to another category using a state like this:

$stateProvider.state('articles.list', {
  url: '{categorySlug}',
    templateUrl: 'partials/article-list.html',
    controller: 'ArticleListCtrl',
    reloadOnSearch: false
  });

That's it. Hope this helps!

Occipital answered 4/4, 2014 at 12:49 Comment(0)
I
3

We're using Angular UI Router instead of built-in routes for a similar scenario. It doesn't seem to re-instantiate the controller and re-render the entire view.

Indecipherable answered 31/8, 2013 at 18:8 Comment(4)
Have you tried using $state.transitionTo() instead of $location.path()?Indecipherable
I don't change $location.path() at all, where should write $state.transitionTo() then?Seyler
Quote: "When a user maximizes a widget, we update the url using location.path, but this is causing the view to re-render." Instead of changing the path, you should call $state.transitionTo() which in turn will update the url hash.Indecipherable
The idea is that you should have two nested states: one for your dashboard, and one for your maximized widget. When you navigate from parent state to the child state, only the child view will re-render, not the parent view.Indecipherable
S
2

How I've implemented it:
(my solution mostly for cases when you need to change whole route, not sub-parts)

I have page with menu (menuPage) and data should not be cleaned on navigation (there is a lot of inputs on each page and user will be very very unhappy if data will disappear accidentally).

  1. turn off $routeProvider
  2. in mainPage controller add two divs with custom directive attribute - each directive contains only 'templateUrl' and 'scope: true'

     <div ng-show="tab=='tab_name'" data-tab_name-page></div>
    
  3. mainPage controller contains lines to simulate routing:

    if (!$scope.tab && $location.path()) {
        $scope.tab = $location.path().substr(1);
    }
    $scope.setTab = function(tab) {
        $scope.tab = tab;
        $location.path('/'+tab);
    };
    

That's all. Little bit ugly to have separate directive for each page, but usage of dynamic templateUrl (as function) in directive provokes re-rendering of page (and loosing data of inputs).

Seyler answered 4/9, 2013 at 13:4 Comment(0)
S
0

If I understood your question right, you want to,

  1. Maximize the widget when the user is on /dashboard/:dashboardId and he maximizes the widget.
  2. You want the user to have the ability to come back to /dashboard/:dashboardId/:maximizedWidgetId and still see the widget maximized.

You can configure only the first route in the routerConfig and use RouteParams to identify if the maximized widget is passed in the params in the controller of this configured route and maximize the one passed as the param. If the user is maximizing it the first time, share the url to this maximized view with the maximizedWidgetId on the UI.

As long as you use $location(which is just a wrapper over native location object) to update the path it will refresh the view.

Selle answered 31/8, 2013 at 19:37 Comment(2)
Not only $location will refresh the view. ng-include will also, even without changing $location.Seyler
@OZ_ I didn't suggest ngInclude since this is just a state change inside a view I'm merely suggesting probably something like ng-if which would be associated to a function which can figure out routeParamsSelle
L
0

I have an idea to use

window.history.replaceState('Object', 'Title', '/new-url');

If you do this and a digest cycle happens it will completely mangle things up. However if you set it back to the correct url that angular expects it's ok. So in theory you could store the correct url that angular expects and reset it just before you know a digest fires.

I've not tested this though.

Lanthanum answered 21/2, 2014 at 16:42 Comment(0)
S
-4

Below code will let you change url without redirection such as: http://localhost/#/691?foo?bar?blabla

for(var i=0;i<=1000;i++) $routeProvider.when('/'+i, {templateUrl: "tabPages/"+i+".html",reloadOnSearch: false});

But when you change to http://localhost/#/692, you will be redirected.

Silica answered 25/1, 2015 at 21:33 Comment(2)
I do not see why adding another answer here.Compute
its a note to the history.Silica

© 2022 - 2024 — McMap. All rights reserved.