Updating URL in Angular JS without re-rendering view

AngularjsAngularjs Routing

Angularjs Problem Overview


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.

Angularjs Solutions


Solution 1 - Angularjs

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
});

Solution 2 - Angularjs

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

Possible duplicate question : https://stackoverflow.com/questions/14974271/can-you-change-a-path-without-reloading-the-controller-in-angularjs

Regards

Solution 3 - Angularjs

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');

Solution 4 - Angularjs

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.

Solution 5 - Angularjs

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!

Solution 6 - Angularjs

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).

Solution 7 - Angularjs

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.

Solution 8 - Angularjs

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.

Solution 9 - Angularjs

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan MuirView Question on Stackoverflow
Solution 1 - Angularjscodef0rmerView Answer on Stackoverflow
Solution 2 - AngularjsbdavidxyzView Answer on Stackoverflow
Solution 3 - AngularjsDaniel GarmoshkaView Answer on Stackoverflow
Solution 4 - AngularjsDreamSonicView Answer on Stackoverflow
Solution 5 - AngularjsohanhiView Answer on Stackoverflow
Solution 6 - AngularjsOZ_View Answer on Stackoverflow
Solution 7 - AngularjsSivakumar KailasamView Answer on Stackoverflow
Solution 8 - AngularjsKeegan 82View Answer on Stackoverflow
Solution 9 - AngularjsabcView Answer on Stackoverflow