Triggering change detection manually in Angular

后端 未结 5 874
滥情空心
滥情空心 2020-11-21 11:47

I\'m writing an Angular component that has a property Mode(): string.

I would like to be able to set this property programmatically not in response to a

5条回答
  •  终归单人心
    2020-11-21 12:11

    Try one of these:

    • ApplicationRef.tick() - similar to AngularJS's $rootScope.$digest() -- i.e., check the full component tree
    • NgZone.run(callback) - similar to $rootScope.$apply(callback) -- i.e., evaluate the callback function inside the Angular zone. I think, but I'm not sure, that this ends up checking the full component tree after executing the callback function.
    • ChangeDetectorRef.detectChanges() - similar to $scope.$digest() -- i.e., check only this component and its children

    You can inject ApplicationRef, NgZone, or ChangeDetectorRef into your component.

提交回复
热议问题