在不更新视图的情况下更改路由参数 [英] Change route parameters without updating view
问题描述
我目前正在尝试弄清楚如何在不重新加载整个页面的情况下更改路由参数.例如,如果我从
开始但将名称更新为George",以将路线更改为:
http://www.example.com/#/page/george
如果我已经有http://www.example.com/#/page/:name路由.
无需重新加载位置.可以设置 $routeParams.name = "George" 吗?
或者,有没有办法更新 http://www.example.com/#/page?name=George 无需重新加载或重置页面?
好的,经过大量的搜索.我回答了我自己的问题.
我发现在 angular 文档中找到任何东西是非常不可能的,但有时,一旦找到,它就会改变您对问题的看法.
我从这里开始:http://docs.angularjs.org/api/ng.$location这把我带到了这里:http://docs.angularjs.org/guide/dev_guide.services.$位置这让我想到了这个问题:AngularJS Paging with $location.path但没有 ngView 重新加载
我最终做了什么:我添加了 $location.search({name: 'George'});
到我想更改名称的地方(A $scope.$watch).
但是,这仍然会重新加载页面,除非您执行底部 StackOverflow 链接中的操作并向传递到 $routeProvider.when
的对象添加参数.就我而言,它看起来像:$routeProvider.when('/page', {controller: 'MyCtrl', templateUrl:'path/to/template', reloadOnSearch:false})
.>
我希望这能让其他人免于头疼.
I'm currently trying to figure out how to change the route parameters without reloading the entire page. For example, if I start at
but update a name to be 'George', to change the route to be:
http://www.example.com/#/page/george
If I already had http://www.example.com/#/page/:name routed.
Without reloading the location. Can one just set $routeParams.name = "George" ?
Edit: Alternatively, is there a way to update http://www.example.com/#/page?name=George without reloading or resetting the page?
Ok, after a lot of searching. I answered my own question.
I've discovered finding anything on the angular documentation is incredibly impossible, but sometimes, once it's found, it changes how you were thinking about your problem.
I began here: http://docs.angularjs.org/api/ng.$location Which took me here: http://docs.angularjs.org/guide/dev_guide.services.$location Which took me to this question: AngularJS Paging with $location.path but no ngView reload
What I ended up doing:
I added $location.search({name: 'George'});
To where I wanted to change the name (A $scope.$watch).
However, this will still reload the page, unless you do what is in that bottom StackOverflow link and add a parameter to the object you pass into $routeProvider.when
. In my case, it looked like: $routeProvider.when('/page', {controller: 'MyCtrl', templateUrl:'path/to/template', reloadOnSearch:false})
.
I hope this saves someone else a headache.
这篇关于在不更新视图的情况下更改路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!