Created
August 25, 2018 18:55
-
-
Save rayonhunte/e31703cbfb190b6ab0f9465d67df6d58 to your computer and use it in GitHub Desktop.
NGXS toast messages with angular 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Message { | |
content: string; | |
style: string; | |
date: number; | |
constructor(content, style) { | |
this.content = content; | |
this.style = style || 'info'; | |
this.date = new Date().getTime(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class='wrapper'> | |
<aside *ngFor="let message of messages$ | async; let i = index"> | |
<div class="alert alert-dismissible fade show" role="alert" | |
[ngClass]="{ | |
'alert-primary': message.style === 'prime', | |
'alert-warning': message.style === 'warn', | |
'alert-danger': message.style === 'danger' | |
}" | |
> | |
<strong>Alert!</strong> {{message.content}} | |
<button type="button" class="close" | |
data-dismiss="alert" aria-label="Close" | |
(click)='onDismiss(i)'> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
</aside> | |
<button (click)="addMessage()">add message</button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Store, Select } from '@ngxs/store'; | |
import {SharedState} from '../shared.state'; | |
import { SendMessage, DismissMessage } from '../shared.actions'; | |
import { Message } from '../message.model'; | |
@Component({ | |
selector: 'app-toast-message', | |
templateUrl: './toast-message.component.html', | |
styleUrls: ['./toast-message.component.css'] | |
}) | |
export class ToastMessageComponent implements OnInit { | |
constructor(private store: Store) { } | |
@Select(SharedState.selMessages) | |
messages$: any; | |
ngOnInit() { | |
} | |
addMessage() { | |
this.store.dispatch(new SendMessage(new Message('test 1', 'danger'))); | |
this.store.dispatch(new SendMessage(new Message('test 2', 'warn'))); | |
this.store.dispatch(new SendMessage(new Message('test 3', 'danger'))); | |
} | |
onDismiss(index: number) { | |
this.store.dispatch(new DismissMessage(index)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment