Skip to content

Commit fefec8c

Browse files
committed
feat(toastr): be able to add custom html
1 parent d1d0f72 commit fefec8c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/toastr.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ angular.module('toastr', [])
44
return {
55
replace: true,
66
template: '<div class="{{toastClass}} {{toastType}}" ng-click="close()">' +
7-
'<div ng-if="title" class="{{titleClass}}">{{title}}</div>' +
8-
'<div class="{{messageClass}}">{{message}}</div>' +
7+
'<div ng-if="title" class="{{titleClass}}" ng-click="fn()">{{title}}</div>' +
8+
'<div ng-switch on="messageType">' +
9+
'<div ng-switch-when="trusted" class="{{messageClass}}" ng-bind-html="message"></div>' +
10+
'<div ng-switch-default class="{{messageClass}}">{{message}}</div>' +
11+
'</div>' +
912
'</div>',
1013
link: function(scope, element, attrs) {
1114
var timeout;
@@ -45,6 +48,7 @@ angular.module('toastr', [])
4548
}])
4649

4750
.constant('toastrConfig', {
51+
allowHtml: false,
4852
containerId: 'toast-container',
4953
extendedTimeOut: 1000,
5054
iconClasses: {
@@ -60,7 +64,7 @@ angular.module('toastr', [])
6064
toastClass: 'toast'
6165
})
6266

63-
.factory('toastr', ['$animate', '$compile', '$document', '$rootScope', 'toastrConfig', '$q', function($animate, $compile, $document, $rootScope, toastrConfig, $q) {
67+
.factory('toastr', ['$animate', '$compile', '$document', '$rootScope', '$sce', 'toastrConfig', '$q', function($animate, $compile, $document, $rootScope, $sce, toastrConfig, $q) {
6468
var container, index = 0, toasts = [];
6569
var containerDefer = $q.defer();
6670

@@ -172,7 +176,13 @@ angular.module('toastr', [])
172176
toast.scope.title = map.title;
173177
}
174178

175-
toast.scope.message = map.message;
179+
if (options.allowHtml) {
180+
toast.scope.messageType = 'trusted';
181+
toast.scope.message = $sce.trustAsHtml(map.message);
182+
} else {
183+
toast.scope.message = map.message;
184+
}
185+
176186
toast.scope.toastType = toast.iconClass;
177187
toast.scope.toastId = toast.toastId;
178188

test/toastr_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ describe('toastr', function() {
1414

1515
beforeEach(function() {
1616
this.addMatchers({
17+
toHaveA: function(tag) {
18+
var el = this.actual.el.find(tag);
19+
return el.length > 0;
20+
},
21+
1722
toHaveClass: function(cls) {
1823
this.message = function() {
1924
return 'Expected "' + this.actual + '"' + (this.isNot ? ' not ' : ' ') + 'to have class "' + cls + '".';
@@ -307,5 +312,12 @@ describe('toastr', function() {
307312
clickToast();
308313
expect($document).toHaveToastOpen(0);
309314
});
315+
316+
it('can show custom html on the toast', function() {
317+
var toast = openToast('success', 'I like to have a <button>button</button>', null, {
318+
allowHtml: true
319+
});
320+
expect(toast).toHaveA('button');
321+
});
310322
});
311323
});

0 commit comments

Comments
 (0)