Updating URL in Angular JS without re-rendering view

前端 未结 9 1881
情书的邮戳
情书的邮戳 2020-12-04 08:33

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 o

9条回答
  •  日久生厌
    2020-12-04 08:52

    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!

提交回复
热议问题