2

I have a problem with dialog from angular material, when i press button to open it, it does, but not rly. Dialog does not show up but console prints 'open' and 'close', no errors

Dialog component

import {Component, Inject} from '@angular/core';
import {RssFeed} from "../model/rssFeed";
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material";
import {AppService} from "../service/app.service";

@Component({
  selector: 'app-new-feed-dialog',
  templateUrl: './new-feed-dialog.component.html',
  styleUrls: ['./new-feed-dialog.component.css']
})
export class NewFeedDialogComponent {

  rssFeed: RssFeed = new RssFeed();

  constructor(private service: AppService,
 public dialogRef: MatDialogRef<NewFeedDialogComponent>,
 @Inject(MAT_DIALOG_DATA) public data: any) {
  }

  onSaveClick(): void {
    this.service.saveRssFeed(this.rssFeed)
    this.dialogRef.close(this.rssFeed);
  }

  onCancelClick(): void {
    this.dialogRef.close();
  }

}

html

<h2 mat-dialog-title>
  <mat-icon>library_add</mat-icon>
  New Feed
</h2>
<mat-dialog-content>
  <form>
    <mat-form-field class="full-width">
      <input matInput placeholder="Feed Name" name="name" [(ngModel)]="rssFeed.name">
    </mat-form-field>
    <mat-form-field class="full-width">
      <input matInput placeholder="Feed Url" name="url" [(ngModel)]="rssFeed.url">
    </mat-form-field>
  </form>
</mat-dialog-content>
<mat-dialog-actions>
  <button mat-button (click)="onCancelClick()">Cancel</button>
  <button mat-button (click)="onSaveClick()">Save</button>
</mat-dialog-actions>

I'm opening it from another component

onAddRssFeedClick(): void{
    let dialogRef = this.dialog.open(NewFeedDialogComponent)
    dialogRef.afterOpen().subscribe( ()=> {
      console.log('open')
    })
    dialogRef.afterClosed().subscribe(() => {
      console.log('close')
    });
  }
Guerric P
  • 30,447
  • 6
  • 48
  • 86
Kamil
  • 23
  • 1
  • 6
  • Did you add NewFeedDialogComponent to your module's entryComponents ? – Guerric P Jun 06 '18 at 14:17
  • @YoukouleleY I did – Kamil Jun 06 '18 at 14:26
  • What exactly happens ? You see the 'close' event, then I assume the overlay appears and you can click it ? – Guerric P Jun 06 '18 at 14:36
  • No, nothing appears, i only click on onAddRssFeedClick() and console prints open and right after that close, nothing appears on the page – Kamil Jun 06 '18 at 14:45
  • possible to upload something on stackblitz ? So far I don't see anything wrong in your code, except the `dialogRef` inside your component that has no reason to be public – Guerric P Jun 06 '18 at 14:48
  • Also I see you're injecting data but not providing it when you open the dialog, did you try removing the `@Inject(MAT_DIALOG_DATA) public data: any`? – Guerric P Jun 06 '18 at 14:51
  • on [stackblitz](https://stackblitz.com/edit/angular-unacyk) it works just fine, – Kamil Jun 06 '18 at 15:42
  • Indeed it works fine on stackblitz... it does not help us then . We must be missing something obvious, I'm going to test on my machine – Guerric P Jun 06 '18 at 15:52
  • It worked before so i must have changed something accidentally :| – Kamil Jun 06 '18 at 16:03
  • @YoukouleleY Whole project [here](https://github.com/Kamil159/RssReader) – Kamil Jun 06 '18 at 17:43

1 Answers1

9

In your app.component.html, there is the button that triggers your modal <a class="nav-link" (click)="onAddRssFeedClick()" href="#">Add Feed</a>

Remove the href="#" and you're good!

Guerric P
  • 30,447
  • 6
  • 48
  • 86