【问题标题】:Angular material components is not working inside `Material Dialog`Angular 材质组件在“材质对话框”中不起作用
【发布时间】:2020-08-07 15:25:13
【问题描述】:

我在对话框中使用mat-form-fieldmat-dialog-closeng-model 属性。但它的抛出错误说它的输入/按钮的未知属性。

title-dialog.html

<h1 mat-dialog-title></h1>
<div mat-dialog-content>
  <p>Enter Column Name</p>
  <mat-form-field>
    <input matInput [(ngModel)]="data.animal" />
  </mat-form-field>
</div>
<div mat-dialog-actions>
  <br />
  <button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>
    Ok
  </button>
</div>

我的组件.ts

import {
    Component,
    OnInit,
    Input,
    OnChanges,
    Output,
    EventEmitter
} from '@angular/core';
import {
    Inject
} from '@angular/core';
import {
    MatDialog,
    MatDialogRef,
    MAT_DIALOG_DATA
} from '@angular/material/dialog';
import {
    MatTableDataSource
} from '@angular/material/table';
import {
    MatSnackBar
} from '@angular/material/snack-bar';


@Component({
    selector: 'app-test',
    templateUrl: './test.html',
    styleUrls: ['./test.scss'],
})
export class MyComponent implements OnInit {

    animal: string;
    constructor() {}
    ngOnInit(): void { this.openDialog(); }

    openDialog(): void {
        const dialogRef = this.dialog.open(TitleDialogComponent, {
            width: '250px',
            data: {
                animal: this.animal
            }
        });

        dialogRef.afterClosed().subscribe(result => {
            console.log('The dialog was closed');
            console.log(result);
        });
    }


}

@Component({
    selector: 'app-title-dialog',
    templateUrl: './title-dialog.html'
})
export class TitleDialogComponent {

    constructor(
        public dialogRef: MatDialogRef < TitleDialogComponent > ,
        @Inject(MAT_DIALOG_DATA) public data) {}

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

}

我在这里遗漏了什么吗?我已在 app.module.ts 中包含所有材料导入,并且它在其他组件中工作正常。

遇到这些错误

编辑 1:

我在这里导入我的材料组件

Material.module.ts

import { NgModule } from '@angular/core';
import { MatStepperModule } from '@angular/material/stepper';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import {MatButtonModule} from '@angular/material/button';
import {MatCardModule} from '@angular/material/card';
import {MatSelectModule} from '@angular/material/select';
import {MatIconModule} from '@angular/material/icon';
import {MatTooltipModule} from '@angular/material/tooltip';
import {MatTableModule} from '@angular/material/table';
import {MatToolbarModule} from '@angular/material/toolbar';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {MatSnackBarModule} from '@angular/material/snack-bar';
import {MatTabsModule} from '@angular/material/tabs';
import {MatListModule} from '@angular/material/list';
import {MatDialogModule} from '@angular/material/dialog';


@NgModule({
  imports: [
    MatStepperModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatCardModule,
    MatSelectModule,
    MatIconModule,
    MatTooltipModule,
    MatTableModule,
    MatToolbarModule,
    MatProgressSpinnerModule,
    MatSnackBarModule,
    MatTabsModule,
    MatListModule,
    MatDialogModule
  ],
  exports: [
    MatStepperModule,
    MatFormFieldModule,
    MatInputModule,
    MatButtonModule,
    MatCardModule,
    MatSelectModule,
    MatIconModule,
    MatTooltipModule,
    MatTableModule,
    MatToolbarModule,
    MatProgressSpinnerModule,
    MatSnackBarModule,
    MatTabsModule,
    MatListModule,
    MatDialogModule
  ]
})
export class MaterialModule {}

我只有一个模块没有子模块,我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from '../app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MyComponent} from '../components/home/home.component';
import { MaterialModule } from './material.module';
import { FlexLayoutModule } from '@angular/flex-layout';
import { RangeSelectionDirective } from '../components/range-selection.directive';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    RangeSelectionDirective
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    MaterialModule,
    BrowserAnimationsModule,
    FlexLayoutModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

【问题讨论】:

  • 你能在 stackblitz.com 中重现这个问题吗?
  • 你能贴一张你的模块导入截图吗?
  • 您错过了相应模块中的导入(您在使用它们的每个模块中都需要它们,即使该模块是您的 app.module 的子模块)。例如。 ngModel 需要 FormsModule。
  • @ArunkumarRamasamy 我无法在 stackblitz 中复制该问题
  • @lionbigcat 我已经编辑了进口问题请检查

标签: angular angular-material


【解决方案1】:

编辑:刚刚注意到您的TitleDialogComponent 未在您的AppModule 中声明。这可能是主要原因-.-

app.module.ts

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    RangeSelectionDirective,
    TitleDialogComponent // <------- Add this
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    AppRoutingModule,
    MaterialModule,
    BrowserAnimationsModule,
    FlexLayoutModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

对于以下内容:最好添加参考。虽然以上应该足以解决您的问题。

您的模块导入似乎正确。也就是说,从错误消息中可以清楚地看出TitleDialogComponent 没有注册任何模块。我对这个问题的猜测是你打开 Modal 的方式,没有堆栈闪电战。

尝试将目标模式引用为其父组件的属性,如下所示:

export class MyComponent implements OnInit {

    animal: string;
    titleDialogRef: MatDialogRef<TitleDialogComponent>; // <------ Added this
    constructor() {}
    ngOnInit(): void { this.openDialog(); }

    openDialog(): void {
        // Assign to titleDialogRef instead
        this.titleDialogRef = this.dialog.open(TitleDialogComponent, {
            width: '250px',
            data: {
                animal: this.animal
            }
        });

        this.titleDialogRef.afterClosed().subscribe(result => {
            console.log('The dialog was closed');
            console.log(result);
        });
    }


}

【讨论】:

  • TitleDialogComponent 添加到app.module 解决了这个问题。非常感谢
猜你喜欢
  • 2020-12-30
  • 1970-01-01
  • 2022-10-13
  • 2019-06-22
  • 2019-07-31
  • 2018-06-17
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
相关资源
最近更新 更多