I need to send the value of a hidden field with ng-value I put the code of which I am sending a text content type
<form>
<div class="comment_it commentupdate">
<div class="up_img">
<img src="" width="35" height="35" />
</div>
<div class="comments-text-post-area">
<input type="hidden" ng-model="c.cid" ng-value="'{{p.id}}'">
<textarea class="add-y-comment" ng-model="c.comment" placeholder="Comentar"></textarea>
</div>
<div class="comment-post-wall">
<div class="cancel-comment">
<button type="button" name="button" id="">CANCELAR</button>
</div>
<div class="send-comment">
<button type="submit" name="button" ng-click="c.addComment()">ENVIAR</button>
</div>
</div>
</div>
</form>
In the Chrome console I only capture the text typed in the textarea. p.id is the value of the post to comment
angular .module('apiFromApp')
.controller('CommentController', CommentController);
CommentController.$inject = ['$http'];
/* @ngInject */
function CommentController($http) {
var self = this;
//sendComment();
self.addComment = function() {
console.log(self);
}
}
This is a known angular issue and there is a simple way to fix it.
I'll start by saying that you shouldn't bind an input of type hidden using ng-value
As time this directive is designed to be used with option elements and input elements of type radio . You are using a hidden type so it won't work.
You should also know that the ng-model directive doesn't work with inputs of type hidden yet. There is an extensive debate on whether to use it or not, but basically and for it to work in all versions of angular you should use the following alternatives.
Use a simple binding on the attribute
value
so that the input has the correct valueIf you want to change the value of an expression to simulate the behavior of ng-model you must use the ng-init directive or assign the expression manually in your controllers
Putting all of the above together in one example
On a personal note I don't really like using ng-init, you should add that in your controllers as these should always be the ones handling the logic and abusing ng-init can create very subtle and hard to find bugs.
Although in the title you say "send values of hidden fields" , the usual thing in an application with AngularJs is that a standard sending of the data of a form is not being carried out, but rather that, from the controller, the values are being obtained to do a call to a service (which will make an AJAX request to the server) .
As a complement to devconcept 's answer, I wanted to provide another solution in which there is neither a form nor a hidden field.
What I would see in the view would be this:
and the controller code would be:
The key to this implementation is the use of
ng-init="c.postId=post.id"
to pass the post identifier to the comment submission controller.Note: As devconcept says, the directive should not be abused
ng-init
but in this case its use could be justified.In addition, when using the directive,
ng-init
it must be taken into account that, when the controller instance is created, the initial values are not yet available.In this case, what can be done is to define a method in the controller that is the one that receives the initial values.
The controller would be changed to:
And the view would look like this:
In addition, in this other way it is even much cleaner (you can see it working in this Plunker )