1

I am trying to adapt an angular contact form found here: http://www.chaosm.net/blog/2014/05/21/angularjs-contact-form-with-bootstrap-and-phpmailer/

It is working perfectly with regular text fields and I get all submitted data by PHP mailer. However, for a specific purpose I need to submit data from hidden input fields. For unknown reason it still works when I pass numeric value using two hidden inputs like this:

<input type="hidden" ng-model="pprice" ng-init="pprice=241.50" value="241.50" />
<input type="hidden" ng-model="formData.inputSubject" id="inputSubject" name="inputSubject" ng-init="formData.inputSubject = pprice" />

Unfortunately this method doesn't work with the text value or an angular expression from another controller - although the value attribute is being rendered correctly, the ng-init does not seem to take it:

<input type="hidden" ng-model="formData.inputSubject" value="{{post.title}}" ng-init="formData.inputSubject=post.title" />

Also none of other solutions I've found so far work: Bind hidden inputs to model in angular Angularjs assigning value to ng-model using ng-init

Why does ng-init accept only numerical values? Why the value attribute expression is not being passed to ng-init in the second case?

I would really appreciate some help as I've spent several hrs trying to resolve this...

Community
  • 1
  • 1
Majick
  • 13
  • 1
  • 4
  • why do you have attribute `value` on your `input` ? you already specify a model. – MamaWalter Feb 23 '15 at 21:52
  • I strongly recommend reading [this first](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background) to understand Angular better. It is not about how to set `ng-init` (which should not even be used for this purpose), as it is about how to use Angular correctly. And I suspect that you are approaching this from a very wrong perspective – New Dev Feb 23 '15 at 22:36
  • agree with @NewDev, basically you can set your model values in the controller like this : $scope.formData.inputSubject = SOME_VALUE; or by getting them from some service / constant: $scope.formData.otherinputSubject = myService.getDefaultValue(); – shershen Feb 23 '15 at 22:48

1 Answers1

2

You're almost there. String values in ng-init need quotes around them.

So that just means that:

ng-init="formData.inputSubject = pprice"

should become:

ng-init="formData.inputSubject = 'pprice'"
Tom Manterfield
  • 6,515
  • 6
  • 36
  • 52