如何检索当前加载到我的 Angular 应用程序中的模板 URL?
How do I retrieve the template URL that is currently loaded in my Angular app?
我想知道 <div ng-view>
中加载了哪个模板 URL,但我不想更改应用程序。原因是我想写一个小书签,returns 当前模板 URL 用于任何带路由的应用程序。
我尝试了 angular.element('[ng-view]').controller()
,但只返回了一个空对象。
或者,我可以做类似
的事情
- 通过从具有
ng-app
指令的元素中获取应用程序来检索应用程序
- 从应用中动态检索
$route
window.alert($route.current.templateUrl)
但是我无法让 1 和 2 工作。
我的问题又来了:
如何检索当前加载到我的 Angular 应用程序中的模板 URL?
没关系,想通了:
// 1
var appInjector = angular.element(document.querySelector('[ng-app]')).injector();
// 2
var route = appInjector.get('$route');
// 3
window.alert($route.current.loadedTemplateUrl);
我想知道 <div ng-view>
中加载了哪个模板 URL,但我不想更改应用程序。原因是我想写一个小书签,returns 当前模板 URL 用于任何带路由的应用程序。
我尝试了 angular.element('[ng-view]').controller()
,但只返回了一个空对象。
或者,我可以做类似
的事情- 通过从具有
ng-app
指令的元素中获取应用程序来检索应用程序 - 从应用中动态检索
$route
window.alert($route.current.templateUrl)
但是我无法让 1 和 2 工作。
我的问题又来了:
如何检索当前加载到我的 Angular 应用程序中的模板 URL?
没关系,想通了:
// 1
var appInjector = angular.element(document.querySelector('[ng-app]')).injector();
// 2
var route = appInjector.get('$route');
// 3
window.alert($route.current.loadedTemplateUrl);