AngularJS - 更新$路由参数的最佳方法 [英] AngularJS - Best way to update $route parameter
问题描述
我有几个途径是不同的,但包含类似的参数。
结果示例:
I have several routes that are different, but contain similar parameters.
Example:
when '/user/:accountId/charts/:chartType', controller:ChartsController
when '/manager/:accountId/charts/:chartType', controller:ChartsController
when '/whatever/pathy/paththingy/charts/:chartType', controller:ChartsController
通知所有三个视图使用相同的图表控制器来控制视图。它是一种非常通用的控制器,但它需要切换可用chartTypes之间......同时保持路径的其余部分。
结果示例(这不起作用)
if my currrent url is '/user/001/charts/comparison'
Then I call something like:
$route.current.params.chartType = 'newChart';
$route.reload(); // or soemthing similar?
I want the url to become '/user/001/charts/newChart'
有没有人有任何想法如何轻松地更新而无需完全重新typeing或重建路由路径路由的参数?
Does anyone have any idea how to easily update parameters of a route without completely re-typeing or rebuilding the route path?
推荐答案
如果有可能,我会建议你使用 UI -router 代替。它比ngRoute更强大是多方面的。
If it's possible, I would recommend you to use ui-router instead. It's more powerful than ngRoute in many aspects.
从本质上讲,该模块有一个名为状态包裹其中底层路线直接从工作低路线的水平一样的路线的URL。
Essentially, this module has a higher level concept called state wrapping the underlying route which shields you from working directly with lower route level like route urls.
有关的比较:<一href=\"http://stackoverflow.com/questions/21023763/difference-between-angular-route-and-angular-ui-router\">Difference角路线和角度的UI路由器之间
使用UI的路由器,可以实现您的code是这样的:
With ui-router, you can implement your code like this:
$stateProvider
.state('user.chart', {
url: '/user/:accountId/charts/:chartType',
controller: "ChartsController"
})
.state('manager.chart', {
url: '/manager/:accountId/charts/:chartType',
controller: "ChartsController"
})
.state('pathy.chart', {
url: '/whatever/pathy/paththingy/charts/:chartType',
controller: "ChartsController"
})
和你可以只使用这code状态之间进行切换:
And you could just use this code to navigate between states:
$state.go('user.chart',{chartType:'newChart'},{inherit:true});
这篇关于AngularJS - 更新$路由参数的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!