最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 2-way binding of textArea in angular js - Stack Overflow

programmeradmin3浏览0评论

I have a list of question that user has to answer.For that a have prepared a form.

The html form is

<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 =='ment'">
      <textarea placeholder="Comments"></textarea>
   </label>
 </div>

now my json string is

{
    "sucess": true,
    "message": "record(s) fetched sucessfully",
    "data": {
        "Question": [
            {
                "id": "4",
                "text": "how was it?",
                "type": "ment"
            },
            {
                "id": "6",
                "text": "how was it?",
                "type": "ment"
            }
        ]
    }
 }

now when user submit the form I should get the ment user has posted of all the question.

I have a list of question that user has to answer.For that a have prepared a form.

The html form is

<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 =='ment'">
      <textarea placeholder="Comments"></textarea>
   </label>
 </div>

now my json string is

{
    "sucess": true,
    "message": "record(s) fetched sucessfully",
    "data": {
        "Question": [
            {
                "id": "4",
                "text": "how was it?",
                "type": "ment"
            },
            {
                "id": "6",
                "text": "how was it?",
                "type": "ment"
            }
        ]
    }
 }

now when user submit the form I should get the ment user has posted of all the question.

Share Improve this question asked Jun 6, 2014 at 10:21 AksAks 8,3015 gold badges38 silver badges39 bronze badges 2
  • 2 What is your question? What isn't working? What have you tried? – StuR Commented Jun 6, 2014 at 10:25
  • I want the answer that user has posted for all the question when click on the submit button. – Aks Commented Jun 6, 2014 at 10:55
Add a ment  | 

2 Answers 2

Reset to default 6

I am not angular expert, but I think you need to add ng-model to the textarea element.

<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 =='ment'">
          <textarea placeholder="Comments" ng-model="question.ments"></textarea>
      </label>
 </div>

And, you also need to add 'ments' field to each ment type question. Example:

        {
            "id": "6",
            "text": "how was it?",
            "type": "ment",
            "ments": ""

        }

Note that you may not need to add "ments" field if there is a 'force add field' flag for angularjs that i'm not aware of.

You have to bind ng-model to the textarea, it works even if you don't have the "answer" variable in your initial json data. I have added a button for demo purpose. Full example on 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 =='ment'">
    <textarea placeholder="Comments" ng-model="question.answer"></textarea>
  </label>
 </div>
 <button ng-click="submit()">Post</button>
</div>
发布评论

评论列表(0)

  1. 暂无评论