我想在 angularjs 中创建一个元素指令,该指令从作为属性传递的 json blob 生成一个 html 元素。我已经尝试了以下几种变体......
demoApp.directive("element", function() {
return {
restrict: "E",
scope: {
attributes: "@",
name: "@"
},
template:
function(name, attributes) {
var templateString = "<" + attributes.tag;
for (attribute in attributes) {
if (attribute != "name_displayed" && attribute != "tag") {
templateString += " " + attribute + "=\"" attributes[attribute] + "\"";
}
}
templateString += " name=\"" field + "\"";
templateString += ">";
templateString += "</" + attributes.tag + ">";
return attributes.name_displayed + ": " + templateString;
}(name, attributes)
};
});
html看起来像
<div ng-repeat="(name, attributes) in fields">
<element name="{[{name}]}" attributes="{[{attributes}]}"></element>
</div>
属性 json 对象的样子
{"name_displayed":"Agency","size":"30","tag":"input","type":"text"}
一个名字看起来像
agency
看起来我无法将函数用于模板,而且看起来我无法访问属性或名称对象。