The reason this doesn't work is described in the docs for the templateUrl
(emphasis mine):
Because template loading is asynchronous the compiler will suspend
compilation of directives on that element for later when the template
has been resolved. In the meantime it will continue to compile and
link sibling and parent elements as though this element had not
contained any directives.
The variable binding that Angular does is a directive and therefore won't complete until after your directive is done loading its template.
As for a work-around, you might need to manually call $compile
yourself instead of relying on templateUrl
to do it for you. See the second note under $compile
for that.
Borrowing some code from this answer, you can do the following in your directive if you use $templateRequest
:
link: function(scope, element, attr){
$templateRequest('js/directives/listar-' + attr.type + '.html').then(function(html){
var template = angular.element(html);
element.append(template);
$compile(template)(scope);
});
};
$templateCache
and $http.get
should also be options with very similar code to the above. That should get around the issue of the value not being interpolated yet.