5

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 not find any reference to this.

MY SOLUTION
Actually I have found solution like this

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

But I don't really like the idea to search through DOM just to get this information.
Do you know a better solution?

1 Answer 1

15

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
    }
  ]
);
Sign up to request clarification or add additional context in comments.

3 Comments

Can I retrieve the $rootElement from a angular service? I need it there, but I don't have a $scope variable in my service.
Simply inject it into your service, the same way you would inject it into controllers. You don't need $scope.
I wasn't able to inject it since I used .service instead of .factory to create my service. Everything works fine now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.