Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how Pass data to angular material bottomsheet

I use Angular Material and Angular 6. I work a lot with material dialog and i make like this :

openDialog3(key : string): void {
  let dialogRef = this.dialog.open(PPSDialogRemoveComponent, {width: '1000px'}); 
  dialogRef.componentInstance.key = key;
}

Now i want to work with angular materialbottomsheet. To pass the key, to my bottom component i try this :

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

But i have this error

ERROR in src/app/geo/geo.component.ts(568,15): error TS2339: Property 'componen Instance' does not exist on type 'MatBottomSheetRef'.

Thanks for your help

like image 767
Newbiiiie Avatar asked Oct 04 '18 10:10

Newbiiiie


People also ask

What is bottom sheet in angular?

Overview for bottom-sheet. The MatBottomSheet service can be used to open Material Design panels to the bottom of the screen. These panels are intended primarily as an interaction on mobile devices where they can be used as an alternative to dialogs and menus. Bottom Sheet Overview.


3 Answers

This might be useful for someone. I needed to add a small table to a bottom sheet so expanding on @YenYees answer:

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;
}

with bottom-sheet-overview-example-sheet.html as

<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>

In stackblitz

like image 36
Woody Avatar answered Oct 24 '22 19:10

Woody


Copy From Angular Material: Sharing data with the bottom sheet component.

If you want to pass in some data to the bottom sheet, you can do so using the data property:

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

Afterwards you can access the injected data using the MAT_BOTTOM_SHEET_DATA injection token:

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) { }
}

Note: Version of Angular Material is v7.0.3

Check this out: Sharing data with the bottom sheet component

like image 89
YEN YEE Avatar answered Oct 24 '22 20:10

YEN YEE


componentInstance property will be applicable only to Dynamic Components which Created using ComponentFactoryResolver method and inserted into the DOM using ViewContainerRef.

As per the Angular Material Doc, dialog.open returns references of the model popup. https://material.angular.io/components/dialog/api

like image 1
Suresh Kumar Ariya Avatar answered Oct 24 '22 20:10

Suresh Kumar Ariya