ui-router没有运行嵌套的路由控制器

时间:2014-12-28 03:57:10

标签: angularjs angular-ui-router

PLNKR:http://plnkr.co/edit/Or4JTiUrPOJUoW78c8Xd

嘿伙计们,我正在努力解决一个问题,我可以简化为以下示例:

var myapp = angular.module('myapp', ["ui.router"])
myapp.config(function($stateProvider, $urlRouterProvider){
    $urlRouterProvider.otherwise("/items/123");

    $stateProvider
        .state('dashboard', {
            url: "/items/:itemId",
            templateUrl: 'dashboard.html',
            controller: function($scope, $stateParams){
              $scope.itemId = $stateParams.itemId;
            },
            resolve: {
                itemParam: ['$stateParams', function($stateParams){
                    return $stateParams.itemId;
                }]
            }
        })
        .state('dashboard.history', {
            parent: 'dashboard',
            url: "/history",
            templateUrl: 'dashboard.history.html',
            controller: function($scope, itemParam){
              $scope.itemId = itemParam.itemId;
            }
        });
})

dashboard.html

<h1>Dashboard for item {{itemId}}</h1>
<a ui-sref="dashboard.history({itemId: 123})">History</a>

dashboard.history.html

<h1>Dashboard History for item {{itemId}}</h1>

问题是历史控制器没有被调用,我没有错误。任何人都可以向我解释一下是什么?

提前致谢!

1 个答案:

答案 0 :(得分:1)

这是因为你的父州内没有<ui-view>指令:

<强> FORKED DEMO

<强> dashboard.html

<h1>Dashboard for item {{itemId}}</h1>
<a ui-sref="dashboard.history({itemId: 123})">History</a>
<ui-view></ui-view>