简体   繁体   中英

Access to AngularJS 1.6 component function from simple outside javascript code

I need to call a function defined in a component of my AngularJS 1.6 app but i need to do this in simple javascript code outside the angular app. How can I do this?

If you want to test something quick and dirty, the function can be accessed from the Developer Console with:

scope = angular.element($0).isolateScope();
scope.$ctrl.myFunction();

Where $0 is set by selecting the component by using right click -> Inspect or clicking the element in the element window of the Developer Console.

For more information, see AngularJS angular.element API Reference - jQuery/jqLite Extras

Using a standard event-broadcasting will help you.

Inside your component:

document.addEventListener('trigger-my-method', () => myMethod());

Somewhere else:

let myEvt = new Event('trigger-my-method'); document.dispatchEvent(myEvt)

Edit: I'm not tested this solution, and i'm not deep into angular, but it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM