Angular controller fetch only changed input values

I have a pretty simple angular controller method:

$scope.addComment = function () {
        if ($scope.newComment.message) {
          $scope.can_add_comments = false;
          new Comments({ comment: $scope.newComment }).$save(function (comment) {
            $scope.comments.unshift(comment);
            return $scope.can_add_comments = true;
          });
          return $scope.newComment = {};
        }
      };

      

And in my form, I have a textbox that contains the value of the comment:

<textarea class="required" cols="40" id="new_comment_message" maxlength="2500" ng-disabled="!can_add_comments" ng-model="newComment.message" required="required" rows="20"></textarea>

      

Works great so far, however I also want to send some data hidden data with a comment. So I added something to keep this value in place:

<input id="hash_id" name="hash_id" ng-init="__1515604539_122642" ng-model="newComment.hash_id" type="hidden" value="__1515604539_122642">

      

However, when I check $scope.newComment

, it is always returned as an object with a single message as its property, which is the value from the text area, and I am not getting a property on the object hash_id

.

When I make this input not hidden and I manually enter a value in the input field and submit the form, I get an object with a property hash_id

. What am I doing wrong here not installing it correctly?

+3


source to share


1 answer


ng-model

Doesn't use the property value

as "default" as far as I know (ie it won't copy it back to your model). If you want the default, it should be placed wherever the model is defined:

$scope.newComment = { hash_id: "__1515604539_122642", /* Other params */ };



Alternatively, changing ng-init

to assignment should work (although I would recommend this solution instead):

<input id="hash_id" name="hash_id" ng-init="newComment.hash_id = '__1515604539_122642'" ng-model="newComment.hash_id" type="hidden">

+2


source







All Articles