javascript - angularjs 状态提供者在 url 中使用除 id 之外的其他值

标签 javascript angularjs html angularjs-routing angularjs-controller

我正在使用 $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/

相关文章:

javascript - Angular Modal,我需要指定关闭行为吗

javascript 对象不会在控制台中打印

javascript - (js) 游戏更新函数中按键的 if 语句

javascript - TinyMCE 4.7.4 不适用于包含查询字符串参数的 URL

javascript - 如何重构我的代码以减少嵌套循环的数量?

javascript - Angularjs: ngDialog 只绑定(bind)和修改对象;不是基本变量。

javascript - 在 Angular/JavaScript 中添加二维数组

javascript - 遍历 AngularJS 中使用 ng-repeat 生成的元素

python - 我不断在 XPath 输出中获取 HTML!我如何获取文本?

javascript - TypeScript 工厂模式表示属性不存在