M
M
moveOn2018-05-27 20:13:51
Angular
moveOn, 2018-05-27 20:13:51

How to pass a template to a directive by parameter in angularjs?

The application has output in ng-repeat, but the elements need to be displayed differently. And you need to separate the display templates from the application code, that is, necessarily in external files. How to pass and process an external template in a directive?
I need it to work like this:

<test template-src="template1.html"></test>
<test template-src="template2.html"></test>

Perhaps it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Mazhekin, 2018-05-28
@mazhekin

Yes, this is possible in Angular 1. The solution is this. All this can be done inside the test directive.
1) All templates are loaded into the cache. To access templates, you need to use the $templateCache service.
The content of the template is translated into a variable
2) Then we need to compile the contents of the template variable.

var scope = {
   var1: 'Переменная в шаблон',
   someEvent: function() {
        alert('Событие из шаблона')
   }
}
var compiledContent = $compile(template)(scope)

3) B And third, insert the compiled code snippet into the DOM.
element.append(compiledContent)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question