【发布时间】:2015-06-20 01:17:31
【问题描述】:
我正在使用$stateProvider 为我的应用规划路线。我希望 url 有意义,这样我就可以得到新闻应用程序的日期、类别和标题,而不是 url 中的 id。
目前我可以在加载所需模板时获得状态
.state('viewArticle', {
url: '/news/:articleId',
templateUrl: 'view-article.html'
})
我得到http://www.example.com/#!/news/5517374f286ff5ba79037568
我想在我的文章中加载相同的视图,显示 URL http://www.example.com/#!/news/2015-04-04/weather/tomorrow
当我在 state 中对 url 进行硬编码时,我可以实现这一点
.state('viewArticle', {
url: '/news/2015-04-04/weather/tomorrow',
templateUrl: 'view-article.html'
})
但我不知道如何从我的数据中将多个参数传递到 url。 这可能吗?如何才能最好地实现这一点。
这是我想出的,但没有成功。
//angular.module().config
.state('viewArticle', {
//url: '/news/:articleId',
//url: '/news/2015-04-04/weather/tomorrow',
url: '/news/:year/:category/:title',
templateUrl: 'view-article.html'
})
//view-article.html (this seems ok as it works when state hard coded)
go('/' + article.date + '/' + article.category + '/' + article.title
//controller
$scope.go = function(path) {
$location.path( path );
};
【问题讨论】:
-
你的意思是
go('/news/' + ar....,对吧? -
@SergiuParaschiv 是的
-
你可能需要在
$location.path之后$scope.$apply()。
标签: javascript angularjs html angularjs-routing angularjs-controller