Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularjs two way data-binding for form data POST
    primarykey
    data
    text
    <p>Using angularjs, I am trying to populate a form dynamically and then submit the forms data via POST to a server.</p> <p>I created a data variable in my controller (to POST later)</p> <pre><code>$scope.data = {}; </code></pre> <p>Then in my html, to create the elements in the form</p> <pre><code>&lt;div ng-repeat="(name, attributes) in fields"&gt; &lt;element myVar="data.{{name}}" name="{{name}}" attributes="{{attributes}}" &gt;&lt;/element&gt; &lt;/div&gt; </code></pre> <p>Where fields looks like</p> <pre><code>{"agency":{"name_displayed":"Agency","size":"30","tag":"input","type":"text"},"department":{"name_displayed":"Department","size":"30","tag":"input","type":"text"},"description":{"cols":"50","name_displayed":"Description","rows":"4","tag":"textarea"}} </code></pre> <p>The element directive then looks like this, but is throwing errors</p> <pre><code>demoApp.directive("element", function() { var template = function(name, attributes, results) { var templateString = "&lt;" + attributes.tag; for (var attribute in attributes) { if (attribute != "name_displayed" &amp;&amp; attribute != "tag" &amp;&amp; attribute != "options") { templateString += " " + attribute + '="' + attributes[attribute] + '"'; } } if (attributes.tag == "input") {templateString += ' value="' + results + '"';} templateString += ' name="' + name + '"&gt;'; templateString += ' ng-model="myVar"&gt;'; if (attributes.tag == "select") { for (var i=0; i&lt;attributes.options.length; i++) { templateString += "&lt;option value=" + attributes.options[i] + ((attributes.options[i] == results)? " selected" : "") + "&gt;" + attributes.options[i] + "&lt;/option&gt;"; } } if (attributes.tag == "textarea") { templateString += results; } templateString += "&lt;/" + attributes.tag + "&gt;"; var toReturn = attributes.name_displayed + ": " + templateString; return toReturn; }; return { restrict: "E", scope: { myVar: '=' }, link: function(scope, element, attrs) { var attributes = angular.fromJson(attrs.attributes); var tpl = template(attrs.name, attributes, attrs.results); element.html(tpl); } }; }); </code></pre> <p>Without the myVar attribute and scope object in the directive, this works fine (to display the form). Am I missing something about the two-way data binding here? Or is there a better way to do this? - Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload