Phillip Chan Phillip Chan - 1 month ago 5
AngularJS Question

AngularJS - How to Get TemplateUrl from Controller?

I'm trying to access the current template URL when I change states in the template's controller.

The routes:

lrdcom.config(
function($stateProvider, $urlRouterProvider, $locationProvider, stringUtilsProvider) {
$stateProvider
.state(
'docs',
{
templateUrl: function($stateParams) {
return 'src/pages/' + $stateParams.doc + '.html';
},
url: '/docs/:doc'
}
);
}
);


In the controller, I'd like to know what the templateURL actually returns:

pageViewer.controller(
'pageViewerController',
function($scope, $location, $state) {
// how to get current url of template?
}
);


As in, if the templateURL returned
src/pages/mydoc.html
how would i get that url from the controller?

I've looked at a number of SO answers suggesting accessing
$route
, or
$state
in the controller but I still cannot get it.

Answer

Have you tried this yet?

$state.current.templateurl

Otherwise do a console.log($state.current) and see if you can find your data there.

Edit:

I just tested it and it works, but note the capital 'U' in 'Url', so like this:

$state.current.templateUrl