Skip to content

Instantly share code, notes, and snippets.

@MeTe-30
Created November 28, 2016 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MeTe-30/ba28805b0805368bd42a9c7e24b54f79 to your computer and use it in GitHub Desktop.
Save MeTe-30/ba28805b0805368bd42a9c7e24b54f79 to your computer and use it in GitHub Desktop.
Better use of $compile and $sce to render html contents | AngularJs
// for html strings that contain expressiona or other angular directives
app.directive('template', ['$compile', function($compile) {
return function(scope, element, attrs) {
attrs.$observe("template", function(_newVal) {
scope.$applyAsync(function() {
element.replaceWith($compile(_newVal)(scope));
});
});
};
}]);
--------------
$scope.html = "<button ng-click="click()">MeTe-30</button>"
<div template="{{html}}"></div>
// for pure html strings
app.filter('sce', ['$sce', function ($sce) {
return $sce.trustAsHtml;
}]);
--------------
$scope.html = "<h1>MeTe-30</h1>"
<div ng-bind-html="html | sce"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment