AngularUI urlRouterProvider.当使用可选参数进行动态路由时

 lucy719674 发布于 2023-02-12 12:38

我正在尝试使用可选参数创建路由; 显然ui.route没有这种能力(即使ngRoute几乎永远存在).所以相反,我已将它们指定为查询参数,并尝试将它们转换为适当的RESTful网址,该网址将针对$ stateParams展开:

在ngRouter语法中,我将指定: /:resource/:collection?/:type?/:index?

在ui.router中,我将其指定为: /:resource?collection&type&index

我试图用一个返回路径的函数动态地翻译它(尝试使用.when()和.rule()):

$urlRouterProvider
.when('' , '/index')
.when('/', '/index')
.when( urlMatcher.source , function convertToREST( $match , $stateParams )
{
    var params = [$match.resource];
    if ( angular.isDefined($match.collection) ) params[1] = $match.collection;
    if ( angular.isDefined($match.type) )       params[2] = $match.type;
    if ( angular.isDefined($match.index) )      params[3] = $match.index;
    var result = '/' + params.join('/');
    return result;
} )
.otherwise( '/404' );

上面导致没有渲染视图(删除第3 .when()和视图渲染就好了).

并手动:

$urlRouterProvider
.when('' , '/index')
.when('/', '/index')
.when( '/:resource?collection' , '/:resource/:collection' )
.otherwise( '/404' );

这显然导致循环迭代.我从ui-router的示例应用程序中获得了这个手动创意:state.js#L16.我看到的唯一区别是我从一个参数开始,但我不明白这是什么重要.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有