当我更改模板控制器中的状态时,我正在尝试访问当前模板URL。
路线:
lrdcom.config(
function($stateProvider, $urlRouterProvider, $locationProvider, stringUtilsProvider) {
$stateProvider
.state(
'docs',
{
templateUrl: function($stateParams) {
return 'src/pages/' + $stateParams.doc + '.html';
},
url: '/docs/:doc'
}
);
}
);
在控制器中,我想知道templateURL实际返回的内容:
pageViewer.controller(
'pageViewerController',
function($scope, $location, $state) {
// how to get current url of template?
}
);
如同,如果templateURL返回src/pages/mydoc.html
,我将如何从控制器获取该网址?
我看了很多SO答案,建议在控制器中访问$route
或$state
,但我仍然无法获得它。
答案 0 :(得分:1)
为什么不将$stateParams
包含为控制器的服务并访问控制器中的doc
参数并再次编写网址?
pageViewer.controller('pageViewerController', function($scope, $location, $stateParams) {
// how to get current url of template?
var url = 'src/pages/' + $stateParams.doc + '.html';
}
);
重要$stateParams
知识
在状态控制器中,$ stateParams对象将只包含在该状态下注册的参数。所以你不会看到在其他州注册的params,包括祖先。
$stateProvider.state('contacts.detail', {
url: '/contacts/:contactId',
controller: function($stateParams){
$stateParams.contactId //*** Exists! ***//
}
}).state('contacts.detail.subitem', {
url: '/item/:itemId',
controller: function($stateParams){
$stateParams.contactId //*** Watch Out! DOESN'T EXIST!! ***//
$stateParams.itemId //*** Exists! ***//
}
})
您还可以在此页URL Routing
上找到上述示例答案 1 :(得分:1)
你试过这个吗?
$ state.current.templateurl
否则执行console.log($ state.current)并查看是否可以在那里找到您的数据。
修改强>
我刚测试了它并且它有效,但请注意'Url'中的首都'U',所以就这样:
$state.current.templateUrl