Javascript 基于目录结构动态设置控制器名称
我正在使用ui.router,我希望能够有一个功能,根据目录结构或URL选择相关控制器。下面是我当前拥有的示例,其中index.main状态的控制器是静态的:Javascript 基于目录结构动态设置控制器名称,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我正在使用ui.router,我希望能够有一个功能,根据目录结构或URL选择相关控制器。下面是我当前拥有的示例,其中index.main状态的控制器是静态的: function config($stateProvider, $urlRouterProvider, $state) { $urlRouterProvider.otherwise("/index/main"); $stateProvider .state('index', { abstract: true,
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' }
})
}
如果函数返回的语句基于当前路由的状态或目录结构,则可能是因为我将在整个应用程序中保持相同的结构
提前谢谢