最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing an angular controller from the console (Chrome or Firefox) - Stack Overflow

programmeradmin5浏览0评论

I have attached a picture of the click action I want to run from the Chrome console, with various values passed. The button is highlighted in gray top right in the inspect element. It is the savePublish() function I want to learn how to access/use.

The button element is inside a <div ng-controller="ProductEditCtrl as ctrl" .. > container, but of course there are other controller wrappers on the page aliased as ctrl

My question is, how do I access the savePublish() method directly from the Chrome console? And a bonus question-answer would be, how would I load the ProductEditCtrl controller and then call the savePublish method on a different page?

I have attached a picture of the click action I want to run from the Chrome console, with various values passed. The button is highlighted in gray top right in the inspect element. It is the savePublish() function I want to learn how to access/use.

The button element is inside a <div ng-controller="ProductEditCtrl as ctrl" .. > container, but of course there are other controller wrappers on the page aliased as ctrl

My question is, how do I access the savePublish() method directly from the Chrome console? And a bonus question-answer would be, how would I load the ProductEditCtrl controller and then call the savePublish method on a different page?

Share Improve this question asked May 7, 2017 at 14:50 Oliver WilliamsOliver Williams 6,3449 gold badges44 silver badges86 bronze badges 2
  • 1 Possible duplicate of How do I access the $scope variable in browser's console using AngularJS? – Matej Marconak Commented May 7, 2017 at 15:09
  • Depending on your background and how you like to debug, you could also use the script debugging tool in the console to set break points in the code and inspect exactly what each variable is at each stage. Sometimes it's easier than console logging everything. – LouisK Commented May 7, 2017 at 15:19
Add a ment  | 

2 Answers 2

Reset to default 11

Try - angular.element($0).scope() to inspect the scope of the button or you can use Chrome extenstion like ng-inspector.

You can find a controller via chrome console, via the elements class name by using angular.element. EG:

angular.element(document.querySelector(".btn.btn-primary")).controller()

Associated methods/functions will be returned, if they are available on that element.

发布评论

评论列表(0)

  1. 暂无评论