I'm having an issue with scope lost when using $compile to create a dynamic template for my directive. See the code below (trimmed for clarity) :
(function () {
'use strict';
angular.module('cdt.dm.directives').directive('serviceSources', ['$http', '$templateCache', '$compile', '$parse',
function ($http, $templateCache, $compile, $parse) {
return {
restrict: 'E',
replace: true,
scope: {
type: '=',
sources: '='
},
link: function (scope, element, attr) {
var template = 'Template_' + scope.type + '.html';
$http.get(template, { cache: $templateCache }).success(function (tplContent) {
element.replaceWith($compile(tplContent)(scope));
});
$compile(element.contents())(scope);
}
}
}
])
})();
that works and the html template is loaded.
the html template looks like:
<table>
<thead>
<tr>
<th>File</th>
</tr>
</thead>
<tbody data-ng-reapeat="src in sources">
<tr>
<td>{{src.fileName}}</td>
</tr>
</tbody>
sources is an array with two elements. In the scope of the directive, it is definitely correct, but in the template, the ng-repeat is not working (I guess because sources is undefined at this stage).
Does someone know what I'm doing wrong ?
templateUrlinstead?scope.$apply. Check my updated answer.