【问题标题】:Angular2 MdDialog doesn't show the content, only show the windowAngular2 MdDialog 不显示内容,只显示窗口
【发布时间】:2017-09-23 03:43:34
【问题描述】:

我尝试使用 MdDialog 来弹出一个窗口。但它只显示一个空白窗口。里面没有内容。

dialog.component.ts:

import {Component, NgModule, ViewContainerRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

import { MaterialModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MdDialog, MdDialogConfig, MdDialogRef } from '@angular/material';

@Component({
    selector: 'dialog',
    template: `
        <h1 md-dialog-title>Dialog</h1>
        <h2>Hi, I am the dialog</h2>
        <button md-raised-button (click)="dialogRef.close()">Close dialog</button>
    `
})

export class DialogComponent {
    constructor(public dialogRef : MdDialogRef<any>){};
}

fileuploader.component.ts

import { Component } from '@angular/core';
import '../../assets/css/styles.css';
import { FileUploader } from 'ng2-file-upload';
import { DialogComponent } from './dialog.component';
import {MdDialog, MdDialogConfig, MdDialogRef} from '@angular/material';
import {NgModule, ViewContainerRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { MaterialModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@Component({
  selector: 'file-uploader',
  templateUrl: '../templates/fileuploader.component.html',
  styleUrls: ['../css/fileuploader.component.css']
})

/** Class representing App Component. */
export class FileUploaderComponent {


  constructor(
    public dialog : MdDialog,
  ) {


      console.log("ImageUpload:uploaded:", item, status, response, headers);
      console.log("test");
      let dialogRef = this.dialog.open(DialogComponent, {
        height: '400px',
        width: '600px',
      });

      dialogRef.afterClosed().subscribe(result => {
        dialogRef = null;
      });

      console.log("done");
    };
  }
}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { AngularFireModule, AuthProviders, AuthMethods } from 'angularfire2';
import { AppComponent } from './components/app.component';
import { LoginComponent } from './components/login.component';
import { DashboardComponent } from './components/dashboard.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { LoggedInGuard } from './common/logged-in.guard';
import { MaterialModule } from '@angular/material';
import { HeaderComponent } from './components/header.component';
import { FileUploaderComponent } from './components/fileuploader.component';
import { FileDropDirective, FileSelectDirective } from 'ng2-file-upload';
import { DialogComponent } from'./components/dialog.component';


/**
 * App Module
 * The app module handles all the imports,
 * declerations, exports and providers. Any new 
 * component need to be declared here.
 */

@NgModule({
  imports: [
    BrowserModule,
    AngularFireModule.initializeApp(firebaseConfig, firebaseAuthConfig),
    RouterModule.forRoot(appRoutes), BrowserAnimationsModule,
    MaterialModule.forRoot()
  ],
  declarations: [
    AppComponent, LoginComponent, DashboardComponent, HeaderComponent,
    FileUploaderComponent, FileSelectDirective, FileDropDirective, DialogComponent
  ],
  entryComponents: [
    DialogComponent
  ],
  exports: [],
  providers: [LoggedInGuard],
  bootstrap: [AppComponent]
})
/** Class representing App Module. */
export class AppModule { }

我应该是弹出一个窗口并在对话框组件中显示消息,“对话框”“嗨,我是一个对话框”。但现在,只是一个空白的窗口。

谢谢。

【问题讨论】:

    标签: angular typescript components mddialog


    【解决方案1】:

    问题在于选择器。似乎“对话框”选择器过于笼统,与材料包有点混淆。将其更改为更具体的对话框。它工作正常。

    【讨论】:

    • 我知道我不应该将它命名为对话框。应该听从我的直觉,感谢您发布此内容,为我节省了几个小时的调试时间。
    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多