Retrieve AngularJS app name

前端 未结 1 1066
轮回少年
轮回少年 2021-01-18 19:03

How can I retrive the name of the current app in AngularJS?
I mean the ng-app=\"foo\" value.
I have searched through the API documentation, but I could

相关标签:
1条回答
  • 2021-01-18 19:42

    From the $rootElement docs:

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

    If you inject it you can get the main module name without scanning the DOM:

    <html ng-app="myApp">
    ...
    
    app.controller('MyCtrl',
      [
        '$scope', 
        '$rootElement', 
        function($scope, $rootElement) {
          console.log($rootElement.attr('ng-app')); // --> myApp
        }
      ]
    );
    
    0 讨论(0)
提交回复
热议问题