md-snackbar
provides a service to provide custom config
. One the properties of config
is extraClasses
that allows to add CSS classes to the snack bar container (doc).
extraClasses
can be used with ::ng-deep
to override the default CSS classes. Here's an example:
component.ts:
Requires following import
in the component:
import {MdSnackBar, MdSnackBarConfig} from '@angular/material';
(providing custom configuration)
openSnackBar(message: string, action?: string) {
let config = new MdSnackBarConfig();
config.extraClasses = ['custom-class'];
this.snackBar.open(message, action ? 'Action Label' : undefined, config);
}
component.css:
::ng-deep snack-bar-container.custom-class {
background: yellow;
}
::ng-deep .custom-class .mat-simple-snackbar {
color: green;
}
Here's a Plunker demo if you would like to try.
NOV 2018 UPDATE: Angular 6+
The syntax has changed a bit, with the md-
prefix being replaced mat-
and extraClasses
was replaced with panelClass
. The function is overall the same though:
const config = new MatSnackBarConfig();
config.panelClass = ['custom-class'];
...
and the imports too:
import { MatSnackBar, MatSnackBarConfig } from '@angular/material';
The verbose, warning, and debug streams are merged into STDOUT when you run PowerShell scripts via
powershell -File "C:\myscript.ps1"
so you can't redirect them separately anymore. Only the error stream is different, since it seems to go to both STDOUT and STDERR, where it can be redirected by 1>
as well as 2>
.
Demonstration:
C:\>type test.ps1
$DebugPreference = "Continue"
$VerbosePreference = "Continue"
Write-Output "Output message"
Write-Error "Error message"
Write-Verbose "Verbose message"
Write-Warning "Warning message"
Write-Debug "Debug message"
C:\>powershell -File .\test.ps1
Output message
C:\test.ps1 : Error message
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
VERBOSE: Verbose message
WARNING: Warning message
DEBUG: Debug message
C:\>powershell -File .\test.ps1 2>nul 3>nul 4>nul 5>nul
Output message
VERBOSE: Verbose message
WARNING: Warning message
DEBUG: Debug message
C:\>powershell -File .\test.ps1 1>nul
C:\>_
If you want to redirect the verbose, warning, or debug stream separately you must use -Command
instead of -File
and do the redirection within PowerShell:
C:\>powershell -Command ".\test.ps1 2>$null 3>$null 5>$null"
Output message
VERBOSE: Verbose message
However, while in CMD you can redirect any handle to any other handle (3>&2
, 1>&5
, ...), PowerShell redirection only supports redirection to either a file (3>C:\out.txt
) or the success output stream (3>&1
). Trying to redirect to any other stream will throw an error:
C:\>powershell -Command ".\test.ps1 2>out.txt 3>&2"
At line:1 char:22
+ .\test.ps1 2>out.txt 3>&2
+ ~~~~
The '3>&2' operator is reserved for future use.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported
as will redirecting different streams to the same file:
C:\>powershell -Command ".\test.ps1 2>out.txt 3>>out.txt"
out-file : The process cannot access the file 'C:\out.txt' because it is being
used by another process.
At line:1 char:1
+ .\test.ps1 2>out.txt 3>>out.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], IOException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
If merging warning and success output is an option for you, you could do something like this:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>error.log"
or like this:
powershell -Command ".\test.ps1 >out.txt 3>&1 2>&1"
or (redirecting all streams) like this:
powershell -Command ".\test.ps1 *>out.txt"
Otherwise the only option I see is to redirect to different files:
powershell -Command ".\test.ps1 3>warning.log 2>error.log"
Best Answer
I'm not sure if this is the "correct" way to do it, but it works, so I'm running with it for now. I'll adapt if there's a better way. My goal was to be able to style non-Material elements (such as standard DIVs, SPANs, etc) with different colors depending on which Material theme was currently applied. It took a combination of Material 2 and Angular 2 elements to make it all work.
Here is what I did: My custom theme file looks like this:
A snippet from my application scss file:
In my "theme" service (although you could do it in any service, as long as it's available globally, or at least anywhere you need it), I defined a simple boolean variable
isDarkTheme
. I use that to control display depending on whether the user has selected the "dark" theme.Then wherever I need to, I use ngClass to apply classes dynamically, depending on the value of the global
isDarkTheme
variable:I have a div wrapping my entire application using the same
ngClass
approach to either apply thedarkTheme
class or not depending on the value of theisDarkTheme
variable. This take care of all Material-aware elements in my entire application in one shot, and I just use thelight-colors
anddark-colors
on the specific non-Material elements where i need to. I could probably combine these, but for now I'm leaving things as-is.For completeness, here are the lists of the elements you can get from the different palettes: From the "primary" palette (
$primary
and$dark-p
in my code above):You can also get these same three color values for the
$accent
and$warn
palettes.From the "foreground" palette (
$light-foreground-palette
and$dark-foreground-palette
in my code above):From the "background" palette (
$light-background-palette
and$dark-background-palette
in my code above):Here are the sources I used to put this together:
I'll freely admit to only understanding about 80% of what's going on here, so if there's a better way, please let me know...