Skip to content
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

Using html in alerts #246

Open
lancegliser opened this issue Jul 24, 2015 · 1 comment
Open

Using html in alerts #246

lancegliser opened this issue Jul 24, 2015 · 1 comment

Comments

@lancegliser
Copy link

Hello!

First, I'd like to thank you for your work. Then gripe or contribute, whichever you deem this. Maybe I just didn't know the proper way to do this it.

I’m attempting to set an error with a bit of html in (simple
).
That goes to the module defined directive of {{alert.msg}}. The alert directive transcludes into another template. Any html I output into that stays html. I’ve tried ng-bind-html on the directive itself: . That works for outputting html correctly, but it destroys the transcluded content additions. I tried doing something like {{$sce.trustAsHtml(testHtml)}}, but that isn’t even evaluated. Comes out as a literal string. Perhaps the template needs to be hit by $compile again to do that?

But, in the mean time modified the directive and template instead:
By placing the message into the scope via attribute, the interior transclude can be replaced with ng-bind-html.

.directive('alert', function () {
  return {
    restrict:'EA',
    controller:'AlertController',
    templateUrl:'template/alert/alert.html',
    transclude:true,
    replace:true,
    scope: {
      type: '=',
      msg: '=',
      close: '&'
    }
  };
});

angular.module("template/alert/alert.html", []).run(["$templateCache", function($templateCache) {
  $templateCache.put("template/alert/alert.html",
    "<div class='alert-box' ng-class='(type || \"\")'>\n" +
    " <div ng-bind-html='msg'></div>\n" +
    "  <a ng-show='closeable' class='close' ng-click='close()'>&times;</a>\n" +
    "</div>\n" +
    "");
}]);

What would be a more proper solution to this issue, without changing the library itself?

@circlingthesun
Copy link
Contributor

Hey. To get around escaping I create a filter as follows:

angular.module('unsafe-filter', [])
.filter('unsafe', function($sce) {
  return function(input) {
    return $sce.trustAsHtml(input);
  };
});

This allows me to write:

<div ng-bind-html='msg|unsafe'></div>

I hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants