2

I must confess that it's not easy to find some basic and easy to understand guide about compiling templates in AngularJS. Here is the deal:

In my main html-page I have this:

<div>
    <div data-ng-include="'./views/testTemplate.html'"></div>
</div>
<div>
    <input type=button ng-click="func()" />
</div>

testTemplate.html contains this:

hello {{myname}}

Im my javascript-controller I have this:

$scope.myname = 'max';  

Now, when I view the page I see the text "hello max".

Im my javascript-controller I also have this:

$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
    $compile(newElem)(newScope);
    console.log('newElem');
    console.log(newElem);
});

In the console I can see this:

newElem
<ng-src><div ng-include="'./views/testTemplate.html'" ></div></ng-src>

So, the template is not getting compiled? What am I missing?

***************EDIT***************

The thing is that Im trying to print to console the content of the new element because it needs to be mailed. So I need to send a mail with the compiled content from the template.

Having looked at the answers below, I now have this:

var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';
var compiledElem = $compile(newElem)(newScope);
console.log('compiledElem[0]');
console.log(compiledElem[0]);

If I use this:

$window.location.href = 'mailto:mailmail.com?subject=sub&body=' + compiledElem[0].innerHTML;

then the body of the mail contains this (uncompiled template):

<!-- ngInclude: './views/matching/testTemplate.html' -->

If I use this:

$window.location.href = 'mailto:mailmail.com?subject=sub&body=' + compiledElem[0];

then the body of the mail contains this:

[object HTMLElement]

So none of them is showing the html-content in the mail I want to send. I know its not exactly the original question, but it was a part of the issue.

2 Answers 2

1

I think the variable 'newElem' is not modified by the $compile command. It has a return value which you should use.

var compiledElement = $compile(newElem)(newScope);
console.log('compiledElement');
console.log(compiledElement);
Sign up to request clarification or add additional context in comments.

3 Comments

Please could you have a look at the edited question?
Could you show what content is in the compiledElement variable? Is it a list of html objects? What content does the objects have?
Actually it only contains a div with text in it. Im now beginning to think about if it is possible to make a call to "mailto" function and send to it an html page. This is probably another question. The best I could do now was to have a timeout in which the mailto-function gets the html() from the compiled code. However, in the body of the mail in my client I only see pure html-code. While I was actually looking for having the real html page in it.
1

You are missing adding your HTML to the DOM.

$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = '<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>';

    //Append to DOM
    document.querySelector('#some-id').append($compile(newElem)(newScope));

    console.log('newElem');
    console.log(newElem);
});

In my example I'm using document.querySelector that is raw js. But we can use the $element service, or if we are in a directive's link function, it receives a param representing the current element where the directive is being applied.

EDIT:

If you want to send your compiled HTML in an email, then, you will need to wait until all the $digest finish to compile your template.

$scope.func = function(){
    var newScope = $scope.$new();
    var newElem = angular.element('<ng-src><div ng-include="\'./views/testTemplate.html\'" ></div></ng-src>');

    $compile(newElem)(newScope);

    $timeout(function(){

        $window.location.href = 'mailto:mailmail.com?subject=sub&body=' + newElem.html();

        //console.log('newElem');
        //console.log(newElem.html());
    });

});

Create your template using angular.element, use $timeout to wait until the end and then use newElem.html();.

2 Comments

Please could you have a look at the edited question?
@oderfla see my edit, you need to wait until angular finish compiling your template.

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.