【问题标题】:angularjs state provider use other values besides id in urlangularjs 状态提供程序在 url 中使用除 id 之外的其他值
【发布时间】: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


【解决方案1】:

我认为你应该使用angular-ui-router 中的$state.go() 而不是$location.path

方法

$state.go('viewArticle',{year: article.date, category: article.category, title: article.title });

【讨论】:

  • @ak85 对你有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多