没有反映在模型

时间:2017-03-25 16:23:53

标签: angularjs ionic-framework

所以我从api中加载了一堆问题,每个问题都有自己的答案。

我无法填充广播框的ng模型以记录所选答案

  

HTML

<div class="list">
    <div class="item item-text-wrap" ng-repeat="question in vm.questions track by $index">
        <h2>{{question.text}}</h2>
        <ion-list if="question.type='mcq'">
          <ion-radio ng-model="vm.answers[$index].answer_text" ng-value="'{{option.text}}'" ng-repeat="option in question.options" name="vm.answers[$index].answer_text">{{option.text}}</ion-radio>
        </ion-list>
        <input type="text" ng-if="question.type=='simple'" ng-model="vm.answers[$index].answer_text" placeholder="{{question.text}}"/>
    </div>
</div>
  

来自控制器的相关代码

vm.answers = [];

api.need_assesment_questions.query(
    //on success
    function success(response) {
        vm.questions = response;

        //populate answers 
        for (var i = 0; i < vm.questions.length; i++) {

            vm.answers.push({ question_text: vm.questions[i].text, answer_text: '' });

        }
    },
    //on error
    function error(response) {
        alert(response.message);
    }
);

我面临的问题是答案数组总是重写第一个答案文字

小提琴:http://jsfiddle.net/pn2qL/76/

1 个答案:

答案 0 :(得分:1)

您需要在顶部初始化问题索引并在模型中使用它,因为还有另一个ng-repeat覆盖了之前的$index

试试这个

<div class="list">
    <div class="item item-text-wrap" 
            ng-repeat="question in vm.questions track by $index" 
            ng-init="qIndex = $index">
        <h2>{{question.text}}</h2>
        <ion-list if="question.type='mcq'">
            <ion-radio ng-model="vm.answers[qIndex].answer_text" ng-value="option.text" ng-repeat="option in question.options" name="{{vm.answers[qIndex].answer_text}}">{{option.text}}</ion-radio>
        </ion-list>
        <input type="text" ng-if="question.type=='simple'" ng-model="vm.answers[$index].answer_text" placeholder="{{question.text}}" />
    </div>
</div>

工作fiddle