mercredi 25 février 2015

Edge-case using ng-model in angularjs forms

I have an annoying edge case that I'm dealing with and would love some help.


The situation:



  • I'm working on an angular app that is a 'theme' for a donation platform; the donation platform allows us to host theme files (images, stylesheets, javascript, html markup, etc.)

  • the platform uses liquid templating to allow use of CSRF tokens as well as to prevent some XSS nastiness.

  • the interpolation provider for angular was changed to // some.expression // so it wouldn't interfere with the liquid brackets {{}}

  • so: a 99:1 mix of html/angular and some liquid.

  • using angular 1.2.27


The problem:




  • the liquid tags also enable us to read data back from the server if a user submits a form and there's an error in it. e.g.: when a user puts their card number in but forgets a field, we can use liquid tags like {{ user.first_name }} to safely access that info and populate the value attributes of fields when the form gets reloaded. That way, users don't face a blank form with errors at the top.




  • for example:



  • <input ng-blur="validateFName=true" ng-required="true" placeholder="First name" type="text" name="donation[first_name]" ng-model="donation.first_name" value="'{{ donation.first_name }}'">


  • becomes:


    <input ng-blur="validateFName=true" ng-required="true" placeholder="First name" type="text" name="donation[first_name]" ng-model="donation.first_name" value="John"> the value attr does get populated, angular is just ignoring it (and that's the intended use, of course)




  • however, because of the way that an ng-model attribute will work, even though the form elements get their value attrs populated, ng-model ignores it and the fields are effectively blank (to the user, anyways)




So: is there any way around this? Essentially, I'm looking for a way to populate the ng-models with the value attribute of their respective field or have ng-model acknowledge the value attribute. I've looked into ng-bind, -init, -value, and others, but most are for more 'normal' usage of angular. And yes, I know this isn't the best way to utilize angular, but I'm bound in this case by my requirements. Any help would be greatly appreciated. Thanks!


Aucun commentaire:

Enregistrer un commentaire