Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 (4) reload template from back-end

I am using some back-end rendered templates in Angular 4 app. And after login I need to reload these templates. Could please someone help with this? I tried something like

import { Component, Compiler  } from '@angular/core';

this.compiler.clearCache();

this.compiler.compileModuleAndAllComponentsAsync(SharedModule);

And I see, that template reloads in the Network tool, but Angular component does't render.

like image 416
John Doe Avatar asked Nov 07 '22 22:11

John Doe


1 Answers

I could not find how to reload a template myself, but I found a workaround:

Using DoCheck().

Explanation:

  1. I basically understand how my component works, what it does onInit() and store that behavior in a directive or method.
  2. With DoCheck() I am constantly listening to model changing and as soon as the model that I am interested on changes, I trigger that directive or method with the functionality of the component. And therefore, the component sort of rerenders.

I implemented this here: Angular2: rendering / reloading a component's template

See my answer, I know it's long, but basically what I did was rerender my menu component when I wanted.

I hope this can be useful... Good Luck!

like image 117
SrAxi Avatar answered Nov 15 '22 12:11

SrAxi