How to force a component's re-rendering in Angular 2? For debug purposes working with Redux i'd like to force a component to re-render it's view, is that possible?
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- How to instantiate Http service in main.ts manuall
- Angular: ngc or tsc?
相关文章
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
- Angular material table not showing data
Rendering happens after change detection. To force change detection, so that component property values that have changed get propagated to the DOM (and then the browser will render those changes in the view), here are some options:
$rootScope.$digest()
-- i.e., check the full component tree$rootScope.$apply(callback)
-- i.e., evaluate the callback function inside the Angular 2 zone. I think, but I'm not sure, that this ends up checking the full component tree after executing the callback function.$scope.$digest()
-- i.e., check only this component and its childrenYou will need to import and then inject
ApplicationRef
,NgZone
, orChangeDetectorRef
into your component.For your particular scenario, I would recommend the last option if only a single component has changed.
tx, found the workaround I needed:
running zone.run will force the component to re-render
Other answers here provide solutions for triggering change detection cycles that will update component's view (which is not same as full re-render).
Full re-render, which would destroy and reinitialize component (calling all lifecycle hooks and rebuilding view) can be done by using
ng-template
,ng-container
andViewContainerRef
in following way:Then in component having reference to both
#outlet
and#content
we can clear outlets' content and insert another instance of child component:Additionally initial content should be inserted on
AfterContentInit
hook:Full working solution can be found here https://stackblitz.com/edit/angular-component-rerender .
ChangeDetectorRef approach