如何在Angularjs中为ng-model添加值

时间:2016-03-17 07:15:49

标签: javascript angularjs angular-ngmodel

如何在angularjs中为ng-model添加值。我的HTML是:

<textarea rows="5" ng-model="comment.comment_text"></textarea>
<input type="hidden" ng-model="comment.task_id" value="{{task.id}}">
<input type="hidden" ng-model="comment.user_id" value="{{profile.id}}">
<button type="submit" class="btn btn-success" ng-click="create(comment)">Send</button>

和角度控制器:

$scope.create = function(comment) {
     console.log(comment);
};

单击按钮在控制台中显示结果时:

{comment_text: "test"}

但我想这样表现出来:

{comment_text: "test", user_id:1 ,task_id:53}

2 个答案:

答案 0 :(得分:2)

在你的控制器的构造函数中,你一定要实例化你的评论的初始值&#39;变量使用:

driver.findElement(By.xpath("//id[contains(text(),'PHONE$')]"));

答案 1 :(得分:1)

value={{someValue}}没有意义。 您应该只使用ng-model进行文本输入隐藏

function Ctrl($scope) {
  $scope.task = {
    id: 1
  }
  $scope.create = function(comment) {
    comment.taskId = $scope.task.id;
    console.log(comment);

  };
}


<div ng-app="">
  <div ng-controller="Ctrl">
    <textarea rows="5" ng-model="comment.comment_text"></textarea>
    <input type="hidden" ng-model="task.id">
    <button type="submit" class="btn btn-success" ng-click="create(comment)">Send</button>
  </div>
</div>

see fiddle

相关问题