I would like to create an element directive in angularjs that produces an html element from a json blob passed as an attribute. I have tried quite a few variations of the following...
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)
};
});
The html looks like
<div ng-repeat="(name, attributes) in fields">
<element name="{[{name}]}" attributes="{[{attributes}]}"></element>
</div>
Where an attributes json object looks like
{"name_displayed":"Agency","size":"30","tag":"input","type":"text"}
And a name looks like
agency
It looks like I cannot use a function for a template, and it also looks like I cannot get access to the attributes or name objects.