Not completely certain if I grasp what you need, but one solution could be to utilize a simple ngIf and zero-timeout method to refresh the component's template:
@Component({
selector: 'hello',
template: `<div *ngIf="show">
... your template
</div>`
})
export class HelloComponent {
public show = true;
// ... your code
reload() {
this.show = false;
setTimeout(() => this.show = true);
}
}
Simply call the reload
function when you wish to "reload" your component. By changing the value of show
to false and allowing for one event loop cycle (setTimeout
), you essentially remove the inner template. Subsequently setting show
back to true triggers the rendering of the inner template once more.
UPD. Additionally, you can prompt Angular to re-render specific data parts within your component's template by manually altering that data:
@Component({
selector: 'hello',
template: `...
<div *ngFor="let i of data">
{{i}}...
</div>`
})
export class HelloComponent {
public data = [];
// ... your code
reloadData() {
this.data = [...this.data];
}
}
This allows you to solely "reload" the data array, causing NgFor
to handle the re-rendering of its inner template automatically. When assigning a new value to this.data
(using spread operator), you instruct Angular to reconstruct the sections of the template bound with data
.