javascript - 在AngularJS中使用routeParams创建templateUrl

标签 javascript angularjs angular-ui-router

是否可以使用路由参数作为路由的一部分?

例如

when('/static/:dynamic', {
    templateUrl: 'partials/static/:dynamic.html',
    controller: 'staticController'
})

我不需要动态 Controller 。我只需要动态解析 URL。

最佳答案

我已经找到了合适的解决方案。

您可以将函数放入 templateUrl 中:

when('/static/:dynamic', {
    templateUrl: function(urlattr){
        return urlattr.dynamic + '.html';
    },
    controller: 'staticController'
})

也可以在路由中添加 *,这样您就可以将整个路径作为一个字符串获取:

when('/:dynamic*', {
    templateUrl: function(urlattr){
        return urlattr.dynamic + '.html';
    },
    controller: 'staticController'
})

使用该解决方案,您可以使用“localhost:13370/app/index.html#/static/dynamic/random”作为 URL,并在 urlattr.dynamic 中获取“static/dynamic/random”作为字符串。

关于javascript - 在AngularJS中使用routeParams创建templateUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27122799/

相关文章:

javascript - TypeScript 构建错误 : Property does not exist on type 'IStateParamsService'

javascript - requirejs with angular - 不解决 Controller 与嵌套路由的依赖关系

AngularJS - Angular UI-Router 仅重新加载嵌套 View ,而不是父 View

angularjs - 在 Ionic 中断显示中插入 Google Adsense 代码

javascript - 文本区域预览

javascript - AngularJS 提供者功能

angularjs - ui-router v1.0.0alpha0 - TypeError : Cannot read property 'creationContext' of undefined

javascript - 检测用户是否会离开像 Bounce Exchange 这样的网站

javascript - SVG 图像在 IE11 中严重缩放

javascript - 如何使用页面滚动进行背景图片切换?任何js或?