Trigger Angular change detection from console

前端 未结 3 1898
北海茫月
北海茫月 2020-12-08 16:42

In AngularJS we were able to trigger a digest cycle by getting the ng-app element with something like

var scope = angular.element(element).scope(); 
scope.$         


        
相关标签:
3条回答
  • 2020-12-08 17:20

    I usually do it as follows in dev mode

    ng.probe(getAllAngularRootElements()[0]).injector.get(ng.coreTokens.ApplicationRef).tick()
    

    Starting with Angular 9 with Ivy you can call:

    ng.applyChanges(ng.getComponent($0))
    

    Where $0 points to component's element

    0 讨论(0)
  • 2020-12-08 17:32

    Solution 1:
    1. Click on the element (on which you want to trigger change detection) in the dev-tools Elements-Tab
    2. ng.probe($0).injector.get(ng.coreTokens.ApplicationRef).tick()

    Solution 2: Trigger a specific components change detector
    1. Click on the element (on which you want to trigger change detection) in the dev-tools Elements-Tab
    2. ng.probe($0).componentInstance.cdRef.detectChanges()

    0 讨论(0)
  • 2020-12-08 17:36

    @guati I was getting some "Failed to Load Module" error (angular 7), the error was triggered due to some changes in the routing but even when I reverted the changes, the error persisted (may be changes were not being detected) so I just restarted the development server (served the application again) and it went away.

    0 讨论(0)
提交回复
热议问题