Instead of using the method open
you must use the method openFromComponent
that allows you to customize the content of Snackbar.
Create a component and a template, follow a small example:
Filing cabinet app.componentts.
import { Component } from '@angular/core';
import { MatSnackBar, MatSnackBarRef } from '@angular/material';
@Component({
selector: 'material-app',
templateUrl: 'app.component.html'
})
export class AppComponent {
constructor(public snackBar: MatSnackBar) {}
/** Método para abrir o Snackbar. */
openSnackBarMensagem(){
this.snackBar.openFromComponent(SnackbarMensagemComponent);
}
}
/** Componente SnackbarMensagem. */
@Component({
selector: 'snackbar-mensagem',
templateUrl: 'snackbar.mensagem.html'
})
export class SnackbarMensagemComponent {
constructor(public snackBarRef: MatSnackBarRef<SnackbarMensagemComponent>) { }
// Método para fechar
closeSnackBarMensagem(){
this.snackBarRef.dismiss();
}
}
Filing cabinet snackbar.messagem.html
<p>Obrigado por ser inscrever</p><a href="" class="link">Clique aqui</a> para logar.
<button mat-button (click)="closeSnackBarMensagem()">Fechar</button>
In the archive app.modulets. you must import the component Snackbarmensagemcomponent
import {AppComponent, SnackbarMensagemComponent} from './app.component';
Continuing in the archive app.modulets., you should declare the same as in the example below:
@NgModule({
imports: [
...
],
entryComponents: [ SnackbarMensagemComponent ],
declarations: [AppComponent, SnackbarMensagemComponent],
...
})
Filing cabinet app.component.html
<p>
<button mat-button (click)="openSnackBarMensagem()">Exibir mensagem</button>
</p>
You can see running on stackblitz.io
Reference
thank you so much for the answer. The problem now is with the close, I’m not able to reproduce the close label in the snackbar. Can you guide me? My code is like this:
– Hitch Leonardo
this.snackBar.openFromComponent(Snackbarmessagecomponent, { Direction: 'Ltr', extraClasses: ['show'], Duration: 50000 } ); --- How can I pass the close label to be displayed on snackbar?
– Hitch Leonardo
@Hitchleonardo edited the answer see how it turned out.
– NoobSaibot