[英]AngularJS - inject string from model to HTML's tag name
是否可以將字符串插入帶有角度的html的標簽名稱中?
像這樣:
<div ng-repeat="type in types">
<bettype-{{type.id}}></bettype-{{type.id}}>
</div>
我需要的輸出是:
<bettype-1></bettype-1>
<bettype-2></bettype-2>
我也在使用聚合物(通過這種方式,我可以創建自定義html標簽)。
我認為最好的解決方案是創建一個創建自定義元素的指令,例如:
.directive('bettype', function($compile) {
return {
restrict: 'E',
compile: function($element, $attr) {
return function($scope, $element, $attr) {
// Create new element here with $attr.number
var number = $attr.number,
element = angular.element('<bettype-'+number+'></bettype-'+number+'>');
// Replace newly created element
$element.replaceWith(element);
$compile($element)($scope);
}
}
}
});
不知道這是否行得通,但可能就是這樣。
注意:我認為用虛線分隔像bettype-1
這樣的元素不是一個好主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.