0

I am new to angular Material and I am working on my first project using it, however I have come to realize that after closing the dialog the page does not refresh, therefore you cannot see the new updates, either a new record you added, a record you updated or deleted, you cannot see it unless you either reload the page manually or via the following code:

openDialog(projectId:number):void{
  
  const dialogRef = this.dialog
  
  .open(UpdateProjectComponent, {
      data:{id: projectId}
  })


  .afterClosed()
  .subscribe((reloadOrNot:boolean)=>{
    
    if(reloadOrNot) window.location.reload();
})

if I change my code as follows nothing happens:

openDialog(projectId:number):void{
  
  const dialogRef = this.dialog
  
  .open(UpdateProjectComponent, {
      data:{id: projectId}
  })


  .afterClosed()
  .subscribe((reloadOrNot:boolean)=>{
    
   if(reloadOrNot) this.projectService.getProjects().subscribe(
      data=>{
        this.projects = data;
      }
    )

})

In the updateProjectComponent all I have is the reactive form to create the form programatically, a method to get the project by id and a method to update. All of these methods work, they do their jobs correctly, my only problem is that once I close the dialog the projects page is not refresh, unless I reload it, which is what I am trying to avoid.

I am calling a Java backend that provides all of the existing projects, however as mentioned before when clicking on submit button in the dialog page, the dialog closes but the projects page does not refresh the data. What would be the best way to handle this without the reload code?

4
  • how is projects been handled in the template HTML? Commented Oct 18, 2022 at 2:49
  • Is your component set to ChangeDetection.OnPush? Commented Oct 18, 2022 at 3:02
  • The UpdateProjectComponent component is a component separate from the projectComponent. Commented Oct 18, 2022 at 4:03
  • I added ChangeDetection.OnPush in the projectsComponent, however when I close the dialog (updateprojectcomponent) nothing happens. I am forced to reload the page to see the changes. All methods work but the refresh part. Commented Oct 18, 2022 at 4:48

1 Answer 1

1

If I understand correctly, we don't see the rest of your code but changes to this.projects = data aren't registered with Angular Change Detection and your template does not refresh. For debugging we need more relevant code which deals with this.projects.

However there are ways to run change dedection cycle.

Using spread:

this.projects = ...data;

https://stackoverflow.com/a/64823904/15439733

Using CD ref

constructor(private cd: ChangeDetectorRef) {}

this.projects = data;
this.cd.dedectChanges();

https://angular.io/api/core/ChangeDetectorRef

Configure OnPush->Default (or remove this line altogether to use Default)

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  changeDetection: ChangeDetectionStrategy.OnPush,
})

https://angular.io/api/core/ChangeDetectionStrategy#OnPush

Sign up to request clarification or add additional context in comments.

6 Comments

I added the changes above in the projectComponent, however when I close the dialog (updateprojectComponent) nothing happens, I still need to reload the page to see the changes.
You seem to be using updateForm I bet you need to update form control values with data.
should ChangeDetection.OnPush be added on projectsComponent or updateprojectcomponent (dialog)? I added it in projectsComponent but did not work.
Joosep, in the updateProjectComponent (dialog) I am populating the form using the data and it works. The idea is that a user click on a project and then the project id is sent to the dialog page where I get the data associated to that project id (that's working well), when users click on the submit button to update the record, the update works but I need to refresh the projectComponent page to see it.
Joosep, I added the ChangeDetection.OnPush in the projectComponent and additionally in the openDialog method in the afterClosed() I added the code where I populate the table using mat-table which uses a datasource. That fixed the problem and now is refreshing correctly without reloading. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.