Answer the question
In order to leave comments, you need to log in
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>
Answer the question
In order to leave comments, you need to log in
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)
element.append(compiledContent)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question