I just want to know how can I rerender the ngFor or refresh a component (hello.component.ts). Instead of text could be images or charts.
Here is an simple example: Stackblitz
Solution:
public show = true;
addData(data){
this.data.push(data);
this.reload();
}
reload() {
this.show = false;
setTimeout(() => this.show = true);
}
By setting show value to false and waiting for 1 event loop is done ( setTimeout ), you destroy inner template. By setting show value to true after this, you render inner template again. This would allow you to "reload" the data array only, so NgFor will handle it's inner template re-rendering automatically.
The very basic approach works by wrapping the element you want to rerender inside a ng-template element that gets rendered into a ng-container . On rerender you can just clear the content of the ng-container and create a new component from the ng-element .
The syntax is *ngFor="let <value> of <collection>" . <value> is a variable name of your choosing, <collection> is a property on your component which holds a collection, usually an array but anything that can be iterated over in a for-of loop.
let user creates a local variable that will be available in the template. of users means that we'll be iterating over the users iterable that should be made available in our component. The * character before ngFor creates a parent template. It's a shortcut to the following syntax: template=“ngFor let item of items” .
Generally, re-rendering of the list occurs in one of three cases: To reduce DOM-manipulation to a bare minimum, angulars' ngFor directive is heavily optimized. For example, if a element is added to the array, it is not re-rendering the whole list. Instead, all the existing DOM-elements are re-used and only the additional element is created.
This would allow you to "reload" the data array only, so NgFor will handle it's inner template re-rendering automatically. By assigning new value to this.data (spread operator), you say to Angular that the parts of the template that are bound with data should be re-built. Hi, Just wanted to know, is this a good practice to reload the component .?
How can I use ngFor? The syntax for the basic use case is straightforward to implement and remember: You can apply the directive to any DOM element that you want to be rendered multiple times depending on a list length. For instance, let's say we want to render such an array of fruits, stored as a component's property:
The very basic approach works by wrapping the element you want to rerender inside a ng-template element that gets rendered into a ng-container. On rerender you can just clear the content of the ng-container and create a new component from the ng-element.
Not sure if I understood your needs, but you may try simple ngIf and zero-timeout approach to re-render 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);
}
}
Call the reload
method when you need to "reload" your component. By setting show
value to false and waiting for 1 event loop is done (setTimeout
), you destroy inner template. By setting show
value to true after this, you render inner template again.
UPD. Also you may force the Angular to re-render data-specific parts of your component's template by changing that data manually:
@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 would allow you to "reload" the data array only, so NgFor
will handle it's inner template re-rendering automatically. By assigning new value to this.data
(spread operator), you say to Angular that the parts of the template that are bound with data
should be re-built.
The challenge is to get angular to see your change and redraw.
I recently worked through this with a coworker and found more than one way to do this. Our issue had to do with filtering
In the StackBlitz you share, you use more than one technique. I isolated just one portion, mutating the array that ngFor uses to draw the DOM elements.
ngFor draw update mutate
I'm also including the issue my coworker and I had. We wanted to have our list of items filtered using the value of an input box. Angular refused to redraw. Our solution was to add an ngModel to the input.
ngFor draw update ngmodel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With