簡體   English   中英

模擬點擊角度元素

[英]Simulate click on angular element

我需要從應用程序外部調用一些Angular控制器函數。 我無法修改Angular應用本身-為簡單起見,代碼應在瀏覽器控制台中運行(最終將集成在Chrome擴展程序中)。

我查看了其他答案-但它們已經過時或根本無法使用。 AngularJS。 如何從控制器組件外部調用控制器功能似乎很有希望,但無法使其正常工作。

鑒於以下HTML:

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

我想從JavaScript調用ctrl.toggleNotesSection() -但在應用程序外部。

更新:我嘗試過的一些事情:

// 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')

我嘗試過的一些事情:

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

AngularJS的生產版本禁用了.scope()方法以提高性能。

如果您希望使用此信息調試應用程序,則應在瀏覽器中打開調試控制台,然后在此控制台中直接調用此方法:

angular.reloadWithDebugInfo();

頁面應該重新加載,並且調試信息現在應該可用。

有關更多信息,請參見$ compileProviderangular.reloadWithDebugInfo上的文檔頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM