使用ng-value

时间:2016-05-10 10:49:33

标签: angularjs angularjs-directive

我需要发送带有ng值的隐藏字段的值我放置了我发送文本内容类型的代码

<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" class="mdl-button mdl-js-button mdl-button--raised" id="" rel="">CANCELAR</button>

              </div>
              <div class="send-comment">

                    <button type="submit" name="button" class="mdl-button mdl-js-button mdl-button--raised" ng-click="c.addComment()">ENVIAR</button>

              </div>
            </div>

          </div>
        </form>

在控制台中,Chrome只会捕获textarea中的书面文字。 p.id是发表评论的价值

角         .module(&#39; apiFromApp&#39)

.controller(&#39; CommentController&#39;,CommentController);

CommentController.$inject = ['$http'];

/* @ngInject */
function CommentController($http) {
    var self = this;

    //sendComment();

    self.addComment = function() {
      console.log(self.cid);
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个

  <input type="hidden" ng-model="c.cid"  ng-value="c.cid = p.id">

&#13;
&#13;
var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
  
  $scope.pid = 4;
  $scope.addComment = function(){
    alert($scope.cid);
    }
  
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   
  <input type="hidden" ng-model="cid"  ng-value="cid = pid">
  <button type="submit" name="button" ng-click="addComment()">ENVIAR</button>
  
</div>
&#13;
&#13;
&#13;