To accomplish this, you can use a directive with a custom forwarding function:
.directive('bindTagName', ['$compile', function ($compile) {
return {
transclude: true,
replace: true,
link: function (scope, elem, attrs, controller, transcludeFn) {
var tagName = scope.$eval(attrs.bindTagName);
var created = angular.element(document.createElement(tagName));
elem.replaceWith(created);
transcludeFn(
function (clone, scope) {
$compile(created.append(clone))(scope);
}
);
}
}
}])
Given the following view:
<div ng-init="tags = ['i', 'b', 't']">
<div ng-repeat = 't in tags' bind-tag-name="t">-> {{t}}</div>
</div>
It outputs the following html:
<div ng-init="tags = ['i', 'b', 't']">
<i><span class="ng-binding ng-scope">-> i</span></i>
<b><span class="ng-binding ng-scope">-> b</span></b>
<t><span class="ng-binding ng-scope">-> t</span></t>
</div>
mathieu
source
to share