如何向Angular Material底部面板传递数据

17
我使用Angular Material和Angular 6。我经常使用材料对话框,我是这样制作的:
openDialog3(key : string): void {
  let dialogRef = this.dialog.open(PPSDialogRemoveComponent, {width: '1000px'}); 
  dialogRef.componentInstance.key = key;
}

现在我想使用Angular Material Bottom Sheet工作。为了将密钥传递给我的底部组件,我尝试了这个:

  openBottomSheet(key: string): void {
    let dialogRef = this.bottomSheet.open(BottomSheetOverviewExampleSheet);
    dialogRef.componentInstance.key = key;
}

但我遇到了这个错误

src/app/geo/geo.component.ts(568,15)中的错误:error TS2339:类型'MatBottomSheetRef'上不存在属性'componentInstance'。

谢谢你的帮助。

3个回答

40

源自Angular Material:使用底部面板组件共享数据。

如果您想将一些数据传递给底部面板,可以使用data属性:

const bottomSheetRef = bottomSheet.open(HobbitSheet, {
  data: { names: ['Frodo', 'Bilbo'] },
});

之后,您可以使用MAT_BOTTOM_SHEET_DATA注入令牌访问注入的数据:

import {Component, Inject} from '@angular/core';
import {MAT_BOTTOM_SHEET_DATA} from '@angular/material';

@Component({
  selector: 'hobbit-sheet',
  template: 'passed in {{ data.names }}',
})
export class HobbitSheet {
  constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any) { }
}

注意:Angular Material的版本为v7.0.3

看这个: 与底部工作表组件共享数据


3
这可能对某些人有用。我需要将一个小表格添加到一个底部工作表中,因此在@YenYees的答案上进行扩展:
import {Component, Inject} from '@angular/core';
import {MatBottomSheet, MatBottomSheetRef} from '@angular/material';
import {MAT_BOTTOM_SHEET_DATA} from '@angular/material';
@Component({
  selector: 'bottom-sheet-overview-example',
  templateUrl: 'bottom-sheet-overview-example.html',
  styleUrls: ['bottom-sheet-overview-example.css'],
})
export class BottomSheetOverviewExample {
  constructor(private bottomSheet: MatBottomSheet) {}

 ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'}
];

  openBottomSheet(): void {
    this.bottomSheet.open(BottomSheetOverviewExampleSheet, {
  data:  this.ELEMENT_DATA ,
});
  }
}

@Component({
  selector: 'bottom-sheet-overview-example-sheet',
  templateUrl: 'bottom-sheet-overview-example-sheet.html',
  styleUrls: ['bottom-sheet-overview-example-sheet.css'],
})
export class BottomSheetOverviewExampleSheet {
  constructor(@Inject(MAT_BOTTOM_SHEET_DATA) public data: any, private bottomSheetRef: MatBottomSheetRef<BottomSheetOverviewExampleSheet>, ) {}
  displayedColumns: string[] = ["position", "name", "weight", "symbol"];
  dataSource =  this.data;

  openLink(event: MouseEvent): void {
    this.bottomSheetRef.dismiss();
    event.preventDefault();
  }
}
export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

以 bottom-sheet-overview-example-sheet.html 为例
<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  <ng-container matColumnDef="position">
    <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="weight">
    <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
  </ng-container>
  <ng-container matColumnDef="symbol">
    <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

stackblitz

1

componentInstance属性仅适用于使用ComponentFactoryResolver方法创建并使用ViewContainerRef插入DOM的动态组件。

根据Angular Material文档,dialog.open返回模态弹出窗口的引用。https://material.angular.io/components/dialog/api


1
我怎样能够在底部表单中传递我的ID? - Newbiiiie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接