Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use {{{}}} syntax for trusted html?

Tags:

angularjs

Handlebar's {{expression}} form HTML-escapes values returned while its {{{expression}}} form does not. Is there any way to add this feature to AngualarJS templates so that we can use {{expression}} for regular sanitized output and {{{expression}}} for trusted, non-escaped HTML expressions?

By the way, I am familiar with ng-bind-html directive.

like image 989
Handsome Nerd Avatar asked Oct 19 '22 17:10

Handsome Nerd


1 Answers

Answer: The short answer is no. I've never come across such a configuration. You can't get {{{}}} to work in Angular.

Helpful workaround: It is not possible to get unescaped/unsanitized HTML into a view through the scope without using the ng-bind-html directive. You could add either a helper function to your controller or add a filter that might make it a little easier to use ng-bind-html (Plunk here), but you still seem to need ng-bind-html:

var app = angular.module('plunker', ['ngSanitize']);

app.controller('MyController', function($scope, $sce) {
  $scope.someHtmlContent = "Label: <input name='test'>";

  $scope.h = function(html) {
    return $sce.trustAsHtml(html);
  };
}); 

app.filter('trustAsHtml', function($sce) { return $sce.trustAsHtml; });

Then you would use it like this:

<body ng-controller="MyController">
  <div ng-bind-html="someHtmlContent | trustAsHtml"> 
  </div>

  <div ng-bind-html="h(someHtmlContent)"> 
  </div>
</body>
like image 86
Michael Oryl Avatar answered Oct 27 '22 11:10

Michael Oryl