-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snackbar + md-icon #4205
Comments
Use a custom snackbar component: import {Component} from '@angular/core';
@Component({
selector: 'custom-snackbar',
template: '<span style="color:white"><md-icon>report_problem</md-icon> Report Problem</span>' // You may also use a HTML file
})
export class CustomSnackbarComponent {}
// Your dependencies here
import {MdSnackbar} from '@angular/material';
import {CustomSnackbarComponent} from './path/to/customsnackbar.component';
constructor(snackbar: MdSnackbar){}
openCustomSnackbar() {
this.snackbar.openFromComponent(CustomSnackbarComponent);
}
@NgModule({
declarations: [
// Your declarations here
CustomSnackbarComponent
],
// Other stuff here
entryComponents: [
CustomSnackbarComponent
]
}) |
As @Chan4077 said, you can use a custom snackbar for anything more than simple text. |
Apologies for bringing this issue back from the dead but I've been trying to find a way to pass some data (ex: a message) to the |
@Chan4077 @jelbourn this works for messages that are statically set, but is there a way to pass in a message similar to I was hoping to create a custom snackbar that was outlined in the docs using the pizza snackbar example (same example provided above), but where it would have similar params to |
@mtpultz @radoslavpetranov Try doing what material2 does for Component for custom snackbar: @Component({
selector: 'warning-snackbar',
template: '<span style="color:orange"><md-icon>alert</md-icon> Warning: {{warningContent}}</span>'
})
export class WarningSnackbarComponent {
warningContent: any;
} Code to show snackbar: showCustomSnackbar(snackbarContent: string) {
let snackbarRef = this.snackbar.openFromComponent(WarningSnackbarComponent);
snackbarRef.instance.warningContent = snackbarContent;
} |
Apparently this is the soon to be new documentation for snackbars, which reflects @Chan4077 response and adds self-referencing - #3002 |
@mtpultz @radoslavpetranov showCustomSnackbar(content: string) {
this.snackbar.openFromComponent(AnotherSnackbarComponent, data: content);
} Snackbar code (< import {Component, Inject} from '@angular/core';
import {MD_SNACK_BAR_DATA} from '@angular/material';
@Component({
selector: 'another-snack-bar',
template: '{{data}}',
})
export class AnotherSnackbarComponent {
constructor(@Inject(MD_SNACK_BAR_DATA) public data: any) { }
} Snackbar code (≥ import {Component, Inject} from '@angular/core';
import {MAT_SNACK_BAR_DATA} from '@angular/material';
@Component({
selector: 'another-snack-bar',
template: '{{data}}',
})
export class AnotherSnackbarComponent {
constructor(@Inject(MAT_SNACK_BAR_DATA) public data: any) { }
} More info: Note: You need to install |
Also, for custom snackbars you need to invoke a private method |
@Chan4077 thanks a lot for the headsup! |
How can I add an action in my custom snackbar (openFromComponent) ? Thank you |
@stoto34 Could you clarify your question? I don't get it. |
@Chan4077 I would like to add a button to close my custom snackbar but I didn't find any parameters to do that. An idea ? |
@stoto34 You can try doing something like my comment above (see #4205 (comment)): (Note: I'll be using a HTML file for the snackbar in this example) @Component({
selector: 'my-snackbar',
templateUrl: 'my-snackbar.component.html'
})
export class MySnackbarComponent {
constructor(private snackBarRef: MatSnackBarRef<MySnackBarComponent>) { }
myContent: any;
snackBarAction: string;
myAction() {
snackBarRef.dismissWithAction();
// Add your functionality in the method that opened the snackbar
}
}
<span>{{myContent}}</span>
<div class="mat-simple-snackbar-action" *ngIf="snackBarAction">
<button (click)="myAction()">{{snackBarAction}}</button>
</div> Note: code is based on showCustomSnackbar(snackbarContent: string) {
let snackBarRef = this.snackbar.openFromComponent(MySnackbarComponent);
snackBarRef.instance.myContent = snackbarContent;
// You can rename the `snackBarAction` attribute to anything you want
snackBarRef.instance.snackBarAction = "Undo";
snackBarRef.onAction().subscribe(() => {
console.log('Action clicked!');
})
} (P.S. If you're installing Angular Material from |
I have to fix some bugs in my code before to test it. Thank you very much for your help :) Edit : it works perfectly ! Thank you again |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug, feature request, or proposal:
expand md-icon into actual icon instead of string in snackbar.open
What is the expected behavior?
Print the icon
What is the current behavior?
Printing text
What are the steps to reproduce?
snackbar.open("report_problem", "", {});
What is the use-case or motivation for changing an existing behavior?
Want to show an icon in front of the text
Which versions of Angular, Material, OS, browsers are affected?
Angular 4.0.2
Material 2.0.0-beta.3
The text was updated successfully, but these errors were encountered: