简体   繁体   English

角材料设计对话框

[英]Angular material design dialog

I'm using the official Angular Material-Design-Components .我正在使用官方的Angular Material-Design-Components But I'm getting into trouble when trying to use the dialog-component.但是我在尝试使用对话框组件时遇到了麻烦。 It simply won't work, though there is an example.尽管有一个例子,但它根本不起作用。 I suppose there must be something missing.我想一定是少了点什么。

If you take a look at the following example right here and view the code, the ts-file contains two components.如果您在此处查看以下示例并查看代码,ts 文件包含两个组件。 The first one refers to the html, which is shown in the example.第一个引用 html,如示例中所示。 But the html of the second component is not shown.但是没有显示第二个组件的 html。 I suppose that there is my problem.我想这是我的问题。 The second component has the following decorator:第二个组件具有以下装饰器:

@Component({
  selector: 'dialog-result-example-dialog',
  templateUrl: 'dialog-result-example-dialog.html',
})

So the dialog-result-example-dialog.html looks like this in my case (this is not part of the example there):所以在我dialog-result-example-dialog.html看起来像这样(这不是那里的例子的一部分):

<h2 md-dialog-title>Delete all</h2>
<md-dialog-content>Are you sure?</md-dialog-content>
<md-dialog-actions>
  <button md-button md-dialog-close>No</button>
  <!-- Can optionally provide a result for the closing dialog. -->
  <button md-button [md-dialog-close]="true">Yes</button>
</md-dialog-actions>

The selector refers to dialog-result-example-dialog .选择器指的是dialog-result-example-dialog But where should this be placed?但是这个应该放在哪里呢? The example seems somewhat incomplete... at least for me as I am a beginner to angular-material.这个例子似乎有些不完整......至少对我来说是一个角度材料的初学者。 Does anyone of you get the example running on his own machine?你们中有没有人让这个例子在他自己的机器上运行? I'd like to know, what I'm missing...我想知道,我错过了什么......

thanks, sheldon谢谢,谢尔顿

I'm gonna do a mini tutorial:我要做一个小教程:

This is the simplest MdDialog you can create:这是您可以创建的最简单的 MdDialog:

import { Component } from '@angular/core';
import { MdDialogRef } from '@angular/material';

@Component({
  selector: 'MyCustomDialog',
  templateUrl: './MyCustomDialog.html',
  styleUrls: ['./MyCustomDialog.css']
})
export class MyCustomDialog{

  constructor(
    public dialogRef: MdDialogRef<MyCustomDialog>,
  ) { }
}

To instantiate it from any other component:要从任何其他组件实例化它:

Step 1: Add your dialog component to the entryComponents array of your app.module.ts步骤1:您的对话框组件添加到entryComponents您的阵列app.module.ts

@NgModule({
  entryComponents: [
    MyCustomDialog,
  ],
})

Step 2: In any component第 2 步:在任何组件中

constructor(private mdDialog: MdDialog){}

openDialog(){
  this.mdDialog.open(MyCustomDialog);
}

NB: As the component is created via the code and not with the template, you don't use its selector and that's also why you have to add the component to the entryComponents array.注意:由于组件是通过代码而不是模板创建的,因此您不使用其选择器,这也是您必须将组件添加到entryComponents数组的原因。

Here is an example.这是一个例子。

Component #1.组件#1。

import { Component, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { SecondDialogComponent } from '../second-dialog/second-dialog.component';

export interface DialogData {
  animals: {};
  likes: {};
}

@Component({
  selector: 'app-first-dialog',
  templateUrl: './first-dialog.component.html',
  styleUrls: ['./first-dialog.component.css']
})
export class FirstDialogComponent implements OnInit {
  // animal: any;
  // like: any;
  // dialogRef: MatDialogRef<SecondDialogComponent>;

  constructor(public dialog: MatDialog) { }

  ngOnInit() { }

  openDialog() {
    this.dialog.open(SecondDialogComponent, {
      hasBackdrop: true,
      data: {
        animals: ['Panda', 'Unicorn', 'Lion'],
        likes: ['Yes', 'No']
      },
      panelClass: 'custom-dialog-container'
    });
  }
}

Component #2组件 #2

import { Component, Inject, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { DialogData } from '../first-dialog/first-dialog.component';

@Component({
  selector: 'app-second-dialog',
  templateUrl: './second-dialog.component.html',
  styleUrls: ['./second-dialog.component.css']
})
export class SecondDialogComponent implements OnInit {

  constructor(public dialogRef: MatDialogRef<SecondDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public dialogData: DialogData) { }

  likesFormControl: FormControl = new FormControl();
  allAnimals: any = this.dialogData.animals;
  allLikes: any = this.dialogData.likes;
  selected: String = "Panda";
  likeSelected: String = 'Yes';
  inputVal: String = "Yes";
  // consolidated: [];
  animal: any;
  like: any;
  saved: boolean = false;

  ngOnInit() { }

  ngAfterViewInit() {
    this.dialogRef.beforeClosed().subscribe((result: { animal: any; like: any; }) => {
      this.animal = result.animal;
      this.like = result.like;
    });
  }

  save() {
      this.saved = true;
    }
  reset() {
    this.saved = false;
    // this.inputVal="yes";
    this.likeSelected = "Yes";
  }

  closeDialog() {
    // this.consolidated = this.allAnimals.slice(0, this.allAnimals.length);
    // this.consolidated = this.allLikes.slice(0, this.allLikes.length);
    this.dialogRef.close();
  }
}

Html for Component #1组件 #1 的 HTML

<div class="container">
    <div class="row justify-content-center">
        <div class="col-lg-4 mt-4 text-center">
            <h4>Click the button to select your favorite animal</h4>
            <button mat-button (click)="openDialog()">Open dialog</button>
        </div>
    </div>
</div>

Html for Component #2组件 #2 的 HTML

<div class="row float-right">
  <mat-icon matSuffix style="cursor: pointer;" (click)="closeDialog()">close</mat-icon>
</div>
<div mat-dialog-title class="mt-4">
  <h1>Favorite Animal</h1>
  <h2>{{selected}}</h2>
</div>
<div mat-dialog-content>
  <div>
    <mat-label>Select an Animal</mat-label>
  </div>
  <mat-form-field appearance="outline">
    <mat-select [(ngModel)]="selected">
      <mat-option *ngFor="let animal of allAnimals" [value]="animal">{{animal}}</mat-option>
    </mat-select>
  </mat-form-field>
  <div>
    <mat-label>Do you like the: {{selected}}?</mat-label>
  </div>
  <mat-form-field appearance="outline">
    <mat-select [(ngModel)]="likeSelected">
      <mat-option *ngFor="let like of allLikes" [value]="like">{{like}}</mat-option>
    </mat-select>
    <!-- <input matInput type="text" [(ngModel)]="inputVal"> -->
  </mat-form-field>
  <div class="justify-content-center mb-4">
    <div *ngIf="likeSelected.toUpperCase() === 'YES'; else label2">{{likeSelected}} I like the {{selected}}</div>
    <ng-template #label2>{{likeSelected}} I don't like the {{selected}}</ng-template>
  </div>
</div>
<div mat-dialog-active>
  <button [disabled]="saved || likeSelected.toUpperCase() === 'NO'" (click)="save()" class="btn btn-primary ml-2"
    type="button">Save</button>
  <button class="btn btn-outline-danger ml-5" type="button" (click)="closeDialog()">Cancel</button>
  <button class="btn btn-outline-success ml-5" type="button" (click)="reset()">Reset</button>
</div>
<div class="justify-content-center mt-4">
  <div *ngIf="saved && likeSelected.toUpperCase() === 'YES'" class="text-success">SAVED</div>
</div>

CSS for Component #2组件 #2 的 CSS

::ng-deep .mat-dialog-container {
    background-color: white !important;
    width: 30em;
    height: 35em;
    text-align: center;
}

::ng-deep .mat-dialog-content {
    font-weight: bold;
    /* color: black; */
}

::ng-deep .mat-dialog-title {
    text-align: center;
    margin-top: 10px;
}

/* ::ng-deep .mat-form-field-appearance-outline {
    background-color: black;
} */

/* ::ng-deep .mat-form-field-underline {
    display: none;
} */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM