0

I have some specific requirement. I have html pages for a functionality (like popup). I want that html to be rendered while changing my state. In this, my state specific HTML is different. Html which I want to render is should get render as a immediate child of body and it contains Angular tag like ng-click etc.

I want to load it by calling a function(manually).

This worked for me :

$http.get("/myTemplate.html").then(function(response) {
    var raw_html = response.data;

    $('body').append($compile(raw_html)(myScope));
});

But as per security concern, I can't use $('body').append (jquery apppend)

So I tried it from pure javascript :

$http.get("/myTemplate.html").then(function(response) {
    var raw_html = response.data;
    var element = document.createElement("div"); 
    element.className = 'myClass';
    element.innerHTML = raw_html;
    document.body.appendChild($compile(element)(currentScope));
});

But what $compile return is not compatible with appendChild.

Can directive help me in this case?

3 Answers 3

1

Why don't you use ng-include.

You can use it in 3 different ways:

<div ng-include="'/myTemplate.html'"></div>
<div ng-include src='/myTemplate.html'"></div>
<ng-include src="'/myTemplate.html'">

If you must go the other way, you can refer to the dom object by element[0]

Edited

document.body.appendChild($compile(element)(currentScope)[0]);
Sign up to request clarification or add additional context in comments.

2 Comments

Because, I want that template at immediate child of body, not under ng-view to override z-index of header and all.
No it is not working because appendChild not accepting $compile(element[0])(currentScope) as argument but $('body').append do.
0

It worked like this document.body.appendChild($compile(element)(currentScope)[0]);

Comments

0

If you can't use jQuery, use angular.element to wrap the body in angular's jqLite instead.

angular.element(document.body).append($compile(raw_html)(myScope));

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.