Note that there are some explanatory texts on larger screens.

plurals
  1. POReset form to pristine state (AngularJS 1.0.x)
    primarykey
    data
    text
    <p>A function to reset form fields to pristine state (reset dirty state) is on the roadmap for AngularJS 1.1.x. Unfortunately such a function is missing from the current stable release.</p> <p><strong>What is the best way to reset all form fields to their initial pristine state for AngularJS 1.0.x.?</strong> </p> <p>I would like to know if this is fixable with a directive or other simple workaround. I prefer a solution without having to touch the original AngularJS sources. To clarify and demonstrate the problem, a link to JSFiddle. <a href="http://jsfiddle.net/juurlink/FWGxG/7/">http://jsfiddle.net/juurlink/FWGxG/7/</a></p> <p>Desired feature is on the <strong>Roadmap</strong> - <a href="http://blog.angularjs.org/2012/07/angularjs-10-12-roadmap.html">http://blog.angularjs.org/2012/07/angularjs-10-12-roadmap.html</a> <br /> <strong>Feature request</strong> - <a href="https://github.com/angular/angular.js/issues/856">https://github.com/angular/angular.js/issues/856</a> <br /> Proposed solution <strong>Pull request</strong> - <a href="https://github.com/angular/angular.js/pull/1127">https://github.com/angular/angular.js/pull/1127</a><br /> <br /></p> <h2>Updated with possible workaround</h2> <p><strong>Good enough workaround?</strong></p> <p>I just figured out I can recompile the HTML part and put it back into the DOM. It works and it's fine for a temporarily solution, but also as @blesh mentioned in the comments:</p> <p><em>Controllers should be used for business logic only, not for DOM!</em> </p> <pre><code>&lt;div id="myform"&gt; &lt;form class="form-horizontal" name="form"&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>And in my Controller on <code>resetForm()</code>:</p> <ul> <li>Save the original untouched HTML </li> <li>Recompile the saved original HTML</li> <li>Remove the current form from the DOM</li> <li>Insert the new compiled template into the DOM</li> </ul> <p>The JavaScript: </p> <pre><code>var pristineFormTemplate = $('#myform').html(); $scope.resetForm = function () { $('#myform').empty().append($compile(pristineFormTemplate)($scope)); } </code></pre>
    singulars
    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.
 

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