首页 文章

角度材质对话框无法打开

提问于
浏览
0

我有一个角度材料的对话问题,当我按下按钮打开它,它确实,但不是rly . 对话框没有显示,但控制台打印'打开'和'关闭',没有错误

对话框组件

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>

我是从另一个组件打开它

onAddRssFeedClick(): void{
    let dialogRef = this.dialog.open(NewFeedDialogComponent)
    dialogRef.afterOpen().subscribe( ()=> {
      console.log('open')
    })
    dialogRef.afterClosed().subscribe(() => {
      console.log('close')
    });
  }

1 回答

  • 0

    在你的 app.component.html 中,有一个按钮可以触发你的模态 <a class="nav-link" (click)="onAddRssFeedClick()" href="#">Add Feed</a>

    删除 href="#" ,你很好!

相关问题