ui-router使用父级url为嵌套视图模板添加前缀

时间:2014-02-17 06:05:01

标签: javascript html angularjs angular-ui-router

我有一个父状态(parent)和一个子状态(parent.child

    $stateProvider
        .state('parent', {
            url: '/parent',
            templateUrl: 'views/parent.html',
            controller: 'parentController',
        })
        .state('parent.child', {
            url: '/:id',
            templateUrl: 'views/child.html',
            controller: 'childController',
        });

父视图包含嵌套的ui-view

ie:views/parent.html

    <h1>Child:</h1>
    <div ui-view></div>

当我更改为子状态时,父状态的网址<{>>前缀到templateUrl

即:templateUrl被解析为/parent/views/child.html

此操作失败,因为/parent/views/child.html不存在。 (该文件存储在/views/child.html

如何配置ui-router不要将父状态的网址添加到templateUrl前面?

1 个答案:

答案 0 :(得分:4)

这不是ui-router问题。您应该使用斜杠添加模板URL,以使其相对于base-url而不是当前url。

所以:

templateUrl : '/views/child.html'