2

I'm trying to get data from my form in AngularJS, this all works fine except for the field I did not type anything in. I changed the field from hidden to text, but both do not work, however if you inspect element you can see the correct value in it. Here's my HTML:

 <div ng-controller="postMessageCtrl as Ctrl">
    <form ng-submit="processMessage()">
        <div class="form-group">
            <input type="message" class="form-control" placeholder="Message" ng-model="formData.message">


            a{{data.receiver.id}}a
            <input type="hidden" class="form-control" ng-model="formData.receiver" ng-value="data.receiver.id" />
        </div>
        <button type="submit" class="btn btn-primary btnq-lg btn-block">Verzenden</button>
    </form>
</div>

And here's my controller:

app.controller('postMessageCtrl', function ($scope, $http, $state, localStorageService) {

    $scope.formData = {};
    //$scope.formData = localStorageService.get('userKey');

    $scope.formData = {
        key: localStorageService.get('userKey'),
        message: '',
        receiver: ''
    };

    console.log($scope.formData);
});

The key and message are filled correctly, but the receiver id is not. any suggestions?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Nick Audenaerde
  • 967
  • 2
  • 14
  • 33

3 Answers3

2

From the answer AngularJS does not send hidden field value:

You cannot use double binding with hidden field. The solution is to use brackets:

<input type="hidden" name="someData" value="{{data}}" /> {{data}}

See this thread on GitHub: https://github.com/angular/angular.js/pull/2574

Since Angular 1.2, you can use ng-value directive to bind an expression to the value attribute of input. This directive should be used with input radio or checkbox but works well with hidden input.

Here is the solution using ng-value:

<input type="hidden" name="someData" ng-value="data" />

Update:

Another solution could be to directly set the value in $scope.formData rather using the hidden input field when you are initializing it:

$scope.formData = {};
//$scope.formData = localStorageService.get('userKey');

$scope.formData = {
    key: localStorageService.get('userKey'),
    message: '',
    receiver: ''
};

$scope.formData.receiver = $scope.data.receiver.id  // Set the value directly in your `formData` since you are using Angular;
console.log($scope.formData);
Community
  • 1
  • 1
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • aint i doing exactly what you say here....? im using ng value, cause i already read that post. – Nick Audenaerde Dec 08 '15 at 10:00
  • But you are using `ng-model` along with the `ng-value`. Can you please remove that and try again. – Shashank Agrawal Dec 08 '15 at 10:01
  • Okay, lets take a step back. How are you populating value in `$scope.data.receiver.id`? – Shashank Agrawal Dec 08 '15 at 10:04
  • i just put everything in scope.formdata, the key is filled through localstorageservice, and the message + receiver are filled through the form. – Nick Audenaerde Dec 08 '15 at 10:07
  • The problem is that this way data will never bind to model which OP needs. Plus ngValue doesn't make sense without ngModel. And even if you add ngModel it will still not work because ngValue is intended for radio buttons and option elements within selectboxes. – dfsq Dec 08 '15 at 10:09
  • That updated version gives me an error cannot read property of id undefined – Nick Audenaerde Dec 08 '15 at 10:23
  • I wrote that just an example. I wanted you to put the actual line there about how you are getting the actual value in `data.receiver.id` which you have written in your HTML hidden element. – Shashank Agrawal Dec 08 '15 at 10:26
  • Yes @NickAudenaerde That's what I asked in my previous comments that how are you getting value in `$scope.data.receiver.id` then you said you filled through local storage. I wanted to fill the value from the local storage in the controller itself (as I've mentioned in the controller) rather doing it in the HTML. – Shashank Agrawal Dec 08 '15 at 10:31
0

The simple solution is to use ngInit directive:

<input type="hidden" class="form-control" 
    ng-model="formData.receiver" 
    ng-init="formData.receiver = data.receiver.id" />
dfsq
  • 191,768
  • 25
  • 236
  • 258