textArea在角度js中的双向绑定

Aks*_*Aks 2 javascript angularjs

我有一个用户必须回答的问题列表.为此,我准备了一个表格.

html表单是

<div class="list" ng-repeat="question in questions.data.Question" > 
      <div class="item item-divider">
         {{question.text}}
  </div>
    <label class="item item-input" ng-if="question.type =='comment'">
      <textarea placeholder="Comments"></textarea>
   </label>
 </div>
Run Code Online (Sandbox Code Playgroud)

现在我的json字符串是

{
    "sucess": true,
    "message": "record(s) fetched sucessfully",
    "data": {
        "Question": [
            {
                "id": "4",
                "text": "how was it?",
                "type": "comment"
            },
            {
                "id": "6",
                "text": "how was it?",
                "type": "comment"
            }
        ]
    }
 }
Run Code Online (Sandbox Code Playgroud)

现在当用户提交表单时,我应该收到用户发布的所有问题的评论.

hol*_*gac 6

我不是角度专家,但我认为你需要在textarea元素中添加ng-model.

<div class="list" ng-repeat="question in questions.data.Question" > 
      <div class="item item-divider">
          {{question.text}}
      </div>
      <label class="item item-input" ng-if="question.type =='comment'">
          <textarea placeholder="Comments" ng-model="question.comments"></textarea>
      </label>
 </div>
Run Code Online (Sandbox Code Playgroud)

而且,您还需要在每个评论类型问题中添加"评论"字段.例:

        {
            "id": "6",
            "text": "how was it?",
            "type": "comment",
            "comments": ""

        }
Run Code Online (Sandbox Code Playgroud)

请注意,如果我不知道的angularjs有一个'force add field'标志,你可能不需要添加"comments"字段.


Sat*_*ish 5

您必须将 ng-model 绑定到 textarea,即使您的初始 json 数据中没有“answer”变量,它也可以工作。我添加了一个用于演示目的的按钮。JSFIDDLE 上的完整示例

<div id="qApp" ng-controller="qAppCntrl">
 <div class="list" ng-repeat="question in questions.data.Question track by $index" > 
  <div class="item item-divider">
     {{question.text}}
  </div>
  <label class="item item-input" ng-if="question.type =='comment'">
    <textarea placeholder="Comments" ng-model="question.answer"></textarea>
  </label>
 </div>
 <button ng-click="submit()">Post</button>
</div>
Run Code Online (Sandbox Code Playgroud)