javascript - angular js中textArea的2路绑定(bind)

标签 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>

现在我的 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"
            }
        ]
    }
 }

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

最佳答案

您必须将 ng-model 绑定(bind)到 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>

关于javascript - angular js中textArea的2路绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079505/

相关文章:

javascript - 如何在 AngularJS 中使用 $location.path 重定向之间的动画?

javascript - RxJS:延迟传入的 observables

javascript - ng-click 不工作不止一次

javascript - CSS Flexbox 显示问题

angularjs - Angular JS 指令有渲染后回调吗?

angularjs 子鼠标悬停边框

angularjs - 前端Angularjs + 后端Jersey REST Services + 需要设置开发环境

javascript - 禁用 KendoGrid 中过滤列的动画

javascript - 正则表达式或管道未按预期工作

javascript - Jquery 将值设置为基于属性的输入字段