Q:
How to automatically refresh an Angular 7 view with the local HTML?
I have a page with a list of items that are generated from a JSON file. On the page I have an add/edit button for each row in the list. Each button creates a new item.
My problem is that, when I edit an item, the edit form doesn't appear until I refresh the page. I tried reloading the page when the add button is clicked, but it isn't the best solution.
Is there a better way to refresh a view automatically when an item is changed?
A:
There's a way to do this using Angular's built in service that's currently being developed and known as ChangeDetectorRef.
What you're going to do is set up a subscription in the component that references this service. You'll need to subscribe to it in the component that's responsible for generating the template you're trying to refresh. So when the template is generating (or retrieving data from, or whatever you're doing) and the component calling changeDetectorRef.detectChanges is called, you're going to want to ensure that this subscription is removed and that the component is refreshed.
Here's some pseudo code:
export class SomeComponent {
subscription: Subscription;
constructor(private changeDetectorRef: ChangeDetectorRef) {
this.subscription = this.changeDetectorRef.value.subscribe(() => {
// Let's suppose that we want to render a list of the items in an *ngFor.
this.listItems = this.items;
});
}
doStuff() {
// Do stuff
}
}
Dakkestienes elatö be359ba680
Related links:
Comments