根据目录结构动态设置控制器名称

时间:2016-09-26 06:59:14

标签: javascript angularjs angular-ui-router

我正在使用ui.router,我希望能够拥有一个根据目录结构或URL选择相关控制器的功能。下面是我目前拥有的一个示例,其中index.main状态的控制器是静态的:

function config($stateProvider, $urlRouterProvider, $state) {
$urlRouterProvider.otherwise("/index/main");

$stateProvider
    .state('index', {
        abstract: true,
        url: "/index",
        templateUrl: "components/index/content.html",
    })
    .state('index.main', {
        controller: 'MainCtrl as controller',
        url: "/main",
        templateUrl: "components/index/main/main.html",
        data: { pageTitle: 'Main' }
    })
}

我只想根据它的路径选择MainCtrl控制器,即:

    $stateProvider
    .state('index', {
        abstract: true,
        url: "/index",
        templateUrl: "components/index/content.html",
    })
    .state('index.main', {
        controller: function(){
            return 'MainCtrl'; // based on the path: components/index/main - this can be based on the state or the URL as well, it just has to follow a similar structure 
        },
        url: "/main",
        templateUrl: "components/index/main/main.html",
        data: { pageTitle: 'Main' }
    })
}

如果函数返回的语句基于当前路由的状态或目录结构,则可能是因为我将在整个应用程序中保持相同的结构。

提前致谢!!

0 个答案:

没有答案