2

AngularJS v1.3.14: Currently, I'm successfully using the 'ngSanitize' module in my angular app with "ng-bind-html" to bind some HTML to the output of an area on the page. That works and doesn't require the older $sce 'trust HTML' stuff.

But, I'd like to embed child bindings within that HTML.

So... something like this works...

angular.module('myApp', ['ngSanitize'...)
    .controller('SomeCtrl', ['$scope', function($scope) {
        $scope.myHTML = '<p>hello world</p>';
}]);
...
<div ng-app="myApp" ng-controller="SomeCtrl" ng-bind-html="myHTML"></div>

Something like this doesn't...

angular.module('myApp', ['ngSanitize'...)
    .controller('SomeCtrl', ['$scope', function($scope) {
        $scope.myContent = 'hello world';
        $scope.myHTML = '<p>{{myContent}}</p>';
}]);
...
<div ng-app="myApp" ng-controller="SomeCtrl" ng-bind-html="myHTML"></div>

Thoughts?

Eric Swanson
  • 429
  • 5
  • 9

1 Answers1

2

A simple directive like this would do it:

<div bindy="myHTML"></div>
.directive('bindy', function($compile) {
  return {
    link: function($scope, $element, $attrs) {
      var html = $scope.$eval($attrs.bindy);
      $element.html(html);
      $compile($element.contents())($scope);
    }
  };
});
m59
  • 43,214
  • 14
  • 119
  • 136