gpt4 book ai didi

angularjs - 检索 AngularJS 应用程序名称

转载 作者:行者123 更新时间:2023-12-04 16:40:02 26 4
gpt4 key购买 nike

如何在 AngularJS 中检索当前应用程序的名称?
我的意思是ng-app="foo"值(value)。
我已经搜索了 API 文档,但找不到任何对此的引用。

我的解决方案
其实我找到了这样的解决方案

angular.element($('[ng-app]')).attr('ng-app');  

但我真的不喜欢通过 DOM 搜索来获取这些信息的想法。
你知道更好的解决方案吗?

最佳答案

来自 $rootElement文档:

The root element of Angular application. This is either the element where ngApp was declared or the element passed into angular.bootstrap.



如果你注入(inject)它,你可以在不扫描 DOM 的情况下获得主模块名称:

<html ng-app="myApp">
...

app.controller('MyCtrl',
[
'$scope',
'$rootElement',
function($scope, $rootElement) {
console.log($rootElement.attr('ng-app')); // --> myApp
}
]
);

关于angularjs - 检索 AngularJS 应用程序名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17502055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com