0

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.

3
  • can you explain what are you exactly trying to achieve? do you want to create a direct? Commented Apr 6, 2016 at 22:47
  • @S4beR i have updated my question. Commented Apr 6, 2016 at 22:56
  • I am still not sure what are you trying to do. can you share some example on jsfiddle or plnkr or any other place Commented Apr 6, 2016 at 23:10

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.