Skip to content

Instantly share code, notes, and snippets.

@monis01
Created January 9, 2018 12:10
Show Gist options
  • Save monis01/eed1c01ecebe49e9ed4f68094d2e80b9 to your computer and use it in GitHub Desktop.
Save monis01/eed1c01ecebe49e9ed4f68094d2e80b9 to your computer and use it in GitHub Desktop.
allowing directive in ng-bind-html
<div ng-app="vkApp">
<div ng-controller="vkController">
<vk></vk>
<div compile="data.article"></div>
</div>
</div>
<script>
angular.module('vkApp', []);
angular.module('vkApp')
.controller('vkController', ['$scope', '$compile', function ($scope, $compile) {
var data = {
article : '<vk></vk>'
}
$scope.data = data;
}]);
angular.module('vkApp')
.directive('vk', ['$compile', function ($compile) {
return {
template: '<div>Content of vk</div>',
restrict: 'E',
link: function(scope, element, attrs){
console.log('"link" function inside directive vk called, "element" param is: ', element)
}
};
}]);
// declare a new module, and inject the $compileProvider
angular.module('vkApp')
.directive('compile', ['$compile', function ($compile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
}]);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment