Created
November 28, 2016 10:24
-
-
Save MeTe-30/ba28805b0805368bd42a9c7e24b54f79 to your computer and use it in GitHub Desktop.
Better use of $compile and $sce to render html contents | AngularJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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