Reputation: 109
I need to declare my html tag(angular directive) as below in javascript. How do i do with correct js syntax.
$scope.myList =[ {title='Angular1' content='<div angular-one></div>'},
{title='Angular2' content='<div angular-two></div>'}];
I will bind this value back to html later. I just want to know how do declare above angular-one and angular-two directives inside the div tag using javascript.
in the html, i will call the passed value from javascript and bind the directive in the html.
{{content}}
my directive get displayed as string like instead of binding as html.
Upvotes: 0
Views: 65
Reputation: 134
It's pretty simple, all what you need it's to create a simple directive to compile the html (using the $compile service from angular):
.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);
}
);
};
}])
You can see a working example here: https://jsfiddle.net/scyrizales/zu2osuzb/
Upvotes: 1