Difference between compiling $ and compiling function in directive in Angular?

I am trying to get my template for a directive using the repository I created that returns a promise that resolves the contents of the template.

What is the difference between using a function compilein a directive and using a service $compilein a function link?

Compilation function

compile: function (element, attrs) {
    templateRepository.get('Shared/Login').then(function (result) {
        element.replaceWith(result);
    });
}

This displays HTML, but the scope is not bound to DOM elements.

Using $ compile

link: function (scope, elem, attrs) {
    templateRepository.get('Shared/Login').then(function (result) {
        elem.html(result);
        $compile(elem.contents())(scope);
    });
}

This works as expected.

What is the difference?

+4
source share
2 answers

$ compile :

Compiles an HTML string or DOM into a template and creates a template function, which can then be used to link the area and template together.

- DOM DOM .

, $compile Angular DOM.

$compile . , , , .

, $compile ( " " ), ( ).

So $compile DOM. - , .

fiddle .

+3

, :

:

, .

(-) - , .

, pre post - , . . .

+1

All Articles