angularjs 状态提供者在 url 中使用除 id 之外的其他值
angularjs state provider use other values besides id in url
我正在使用 $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
的文章相同的视图
当我在状态 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 );
};
我认为你应该使用 $state.go()
或 angular-ui-router
而不是 $location.path
方法
$state.go('viewArticle',{year: article.date, category: article.category, title: article.title });
我正在使用 $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
当我在状态 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 );
};
我认为你应该使用 $state.go()
或 angular-ui-router
而不是 $location.path
方法
$state.go('viewArticle',{year: article.date, category: article.category, title: article.title });