我正在使用 $stateProvider
为我的应用绘制路线。我希望 url 有意义,这样我就可以获取新闻应用程序的日期、类别和标题,而不是通过 url 中的 id。
目前我可以在加载所需模板时获取状态
.state('viewArticle', {
url: '/news/:articleId',
templateUrl: 'view-article.html'
})
我得到 http://www.example.com/#!/news/5517374f286ff5ba79037568
我想加载与我的文章相同的 View ,显示 url http://www.example.com/#!/news/2015-04-04/weather/tomorrow
当我在状态中硬编码 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 );
};
最佳答案
我认为你应该使用 angular-ui-router
的 $state.go()
而不是 $location.path
方法
$state.go('viewArticle',{year: article.date, category: article.category, title: article.title });
关于javascript - angularjs 状态提供者在 url 中使用除 id 之外的其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627185/