是否有AngularJS动态路由? [英] Does AngularJS have dynamic routing?
问题描述
角是否支持动态路由呢?
也许一些绝招是这样的:
Does angular support dynamic routing at all? Maybe some trick like this:
$routeProvider.when('/:ctrl/:action',
getRoute($routeParams.ctrl,$routeParams.action))
function getRoute(ctrl, action){
return {
templateUrl: ctrl+"-"+action+".html"
controller: 'myCtrl'
}
}
请帮助我,我需要根据出routeParams来获取templateUrl
Please help me, I need to get templateUrl based out of routeParams
推荐答案
这是一个迟到的答案,但我碰到这个问题我自己来了,但事实证明,丹解决方案与上ngView指令ngAnimate类冲突,而视图显示,但NG休假动画将立即应用和隐藏的观点与他的动态路由打开。
This is a late answer but I came across this problem myself, but it turns out that the solution by Dan conflicts with ngAnimate classes on the ngView directive, and the view is shown but the ng-leave animation will immediately be applied and hide the view opened with his dynamic routing.
我找到了完美的解决方案这里,而且它在1.1.5可用+
I found the perfect solution here, and it's available in 1.1.5 +
在 $ routeProvider
的 templateUrl
值可以是一个函数,并传递路线参数:
In the $routeProvider
, the templateUrl
value can be a function, and is passed the route parameters:
app.config(function ($routeProvider) {
$routeProvider
.when('/:page', {
templateUrl: function(routeParams){
return '/partials/'+routeParams.page+'.html';
}
})
});
虽然控制器不能给出一个函数,所以我的解决办法是给它的HTML模板按通常用 NG-控制器=HomeCtrl
。
使用该解决方案通过约定角度我们可以路线。
我希望这可以帮助其他人谁不热衷于每条路由手动添加到routeProvider。
Using this solution we can route by convention in Angular. I hope this helps others who weren't keen on manually adding every route to the routeProvider.
这篇关于是否有AngularJS动态路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!