0

I need to call some Angular controller functions from outside the app. I can't modify the Angular app itself - for sake of simplicity, the code should run in the browser console (the final will be integrated in a Chrome Extension).

I looked at other answers - but they are outdated or simply do not work. AngularJS. How to call controller function from outside of controller component seemed promising, but can't get it to work.

Given the following HTML:

<div data-ng-if="::ctrl.isEntityNotesEnabled"
     data-ng-class="{'menu__item--enabled': ctrl.isNotesShown}"
     data-ng-click="ctrl.toggleNotesSection()" role="button">
  Show 
</div>

I want to call ctrl.toggleNotesSection() from JavaScript - but outside of the app.

UPDATE: Some things I tried:

// this will return undefined, old AngularJS approach
angular.element('ng-controller-selector').scope()
// seems a bit closer to what I want, but "ctrl" isn't exposed, nor toggleNotesSection()
angular.element('ng-controller-selector').injector().get('$rootScope')
// I also tried using the DIV element, without success
angular.element('div selector')
8
  • the answer linked is pretty much the only way it can work if you can't modify the app to put in a proper listener. see here for the same answer: stackoverflow.com/questions/22795628/… Commented Jun 27, 2019 at 16:15
  • stackoverflow.com/questions/35296704/… check this Commented Jun 27, 2019 at 16:16
  • @samnupel stuff like that works, but requires modifying the angular application. Commented Jun 27, 2019 at 16:17
  • @bryan60 - this seems to be for older versions of Angular, can't get this to work - do you have example code? Commented Jun 27, 2019 at 17:17
  • @samnupel - sorry, I'm looking at the accepted solution and others, can't figure it out. Also, it is possible to "attach" to angular from outside. In another part of my code, I'm doing this: const injector = angular.element(document.body).injector(), someService = injector.get("someService"); someService.update(data); So I feel I'm close to the solution... Commented Jun 27, 2019 at 17:18

1 Answer 1

0

Some things I tried:

// this will return undefined, old AngularJS approach
angular.element('ng-controller-selector').scope()

The production version of AngularJS has the .scope() method disabled in order to boost performance.

If you wish to debug an application with this information then you should open up a debug console in the browser then call this method directly in this console:

angular.reloadWithDebugInfo();

The page should reload and the debug information should now be available.

For more see the docs pages on $compileProvider and angular.reloadWithDebugInfo.

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

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.