29

Is there a way to find the controller for an element through Chrome's console? I can get a reference to the component by selecting the element in the Elements Panel and using

var c = angular.element($0);

c has a controller property (looks like a constructor), but I'm not sure what to do with this. Is there any way to find the controller's name from here?

2
  • are you just trying to get the controller name, or are you trying to use the functions in the controller and the items on the scope. Commented Feb 24, 2014 at 18:23
  • @JaredReeves just the controller name. Commented Feb 25, 2014 at 0:59

5 Answers 5

23

If you do angular.element($0).scope() or just $scope(if you installed Batarang Chrome extension), you should be able to get access to selected element's scope's functions and properties. This should also include any functions/attributes that a controller has exposed on the scope.

There is no way to get the controller's name, though.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to get the name of the controller from its scope?
Not unless the scope 'requires' a controller. What is your use case for accessing a controller from the console?
I am just curious if it's possible.
The ".scope()" on element can be disabled if compileProvider.debugInfoEnabled(false); according to docs.angularjs.org/guide/production
14

Assuming you're not using anonymous functions for a controller you can use something like this:

angular.element(element).controller().constructor.name

codepen - http://codepen.io/jusopi/pen/jWYWzv?editors=101

At least as of Angular 1.2.27, per their documentation you can do this. Look under the JQLite Extras Method section - https://docs.angularjs.org/api/ng/function/angular.element

Comments

9

This works with Angular 1.5's components

Assuming you have a component named ProductViewComponent which is handled by ProductViewController, then getting the controller is easy:

angular.element("product-view").controller("productView")

Remarks:

  • Note lower case letters and dash in element name
  • and lower case and no "Controller" suffix in controller text

Examples

Now you can play with it, for instance invoke methods:

angular.element("product-view").controller("productView").hasProducts();

Or invoke actions!

angular.element("product-view").controller("productView").products = [ 'abc' ]
angular.element("product-view").scope().$apply()

2 Comments

This works regardless of the controller name. The text in the call to element.controller() just needs to be the camelCase name of the component. Where did you find this magic piece of information? I've been searching all morning until I stumbled across this gem.
@burns - this code is a part of component tests in angular (jasmine tests for a component). I just being wandering whether the same should work "in real" browser.
5

As @jusopi already pointed out you can use the jqLite extra methods angular.element(element).controller(componentName):

[It] retrieves the controller of the current element or its parent. By default retrieves controller associated with the ngController directive. If name is provided as camelCase directive name, then the controller for this directive will be retrieved

If you provide a directive/component name in camelCase you can also get specific controllers of all directives/components in the selected element:

angular.element($0).controller('MyComponent')

($0 refers to the the most recent selection in the elements inspector)

1 Comment

I wish they'd highlight that more in the docs. I just learned about this from your answer. Thanks.
1

If you are just looking to find out what the controller is (and not use it in your code, maybe just the console):

run angular.element(element).controller().constructor

in the console, and if the function is named, you should be able to see the name. (as mentioned by @jusopi)

even if it's not, though, if you're in the chrome devtools (probably other tools too), you can right click on the returned nameless function and

select "Show Function Definition" in the right-click menu

which will take you to the place in the source code where it's defined, and from the context there, you can figure out what controller it is, what functions are defined within it, etc.

Comments

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.