繁体   English   中英

AngularJS嵌套ng-repeat

[英]AngularJS nested ng-repeat

我有包含问题和选项列表的对象。 我使用嵌套的ng-repeats在页面上显示此对象。 当我尝试更改“问题”时,一切都很好,但是当我尝试更改“选择”时,什么也没有发生。 问题出在哪儿?

我的页面:

<div class="panel">

<div class="row">

    <div class="large-12 columns">

        <h3>{{ title }}</h3>

            <ol>
                <li ng-repeat="q in quiz">
                    <input type="text" ng-model="q.question">
                    <ul class="remove-li-dots">
                        <li ng-model="choice" ng-repeat="choice in q.choices">
                            <input type="text" ng-model="choice" name="answer{{ q.id }}" >
                        </li>
                    </ul>
                    </br>
                </li>
            </ol>
            <a class="button" ng-click="submitQuiz()">Submit</a><br>
            {{ quiz }}
    </div>

</div>

页面截图:

https://www.dropbox.com/s/i52fwq1os0cvcr9/repeat.png?dl=0

问题是选择是一个字符串,因此在子范围内更改它时,它仅在子范围内更改。

要解决此问题,请通过ng-repeat="(choiceIndex,choice) in q.choices数组中的索引来引用它, ng-model="q.choices[choiceIndex]"

另外,为防止更改项目时输入失去焦点-您将需要在ng-repeat指令中track by $index添加track by $index

<ol> <li ng-repeat="q in quiz"> <input type="text" ng-model="q.question"> <ul class="remove-li-dots"> <li ng-model="choice" ng-repeat="(choiceIndex,choice) in q.choices track by choiceIndex"> <input type="text" ng-model="q.choices[choiceIndex]" name="answer{{ q.id }}"> </li> </ul> </li> </ol>

工作示例: http : //plnkr.co/edit/GJacjkzfT4FpfC6szsNk?p=preview


为了更好地了解角度范围的工作原理,我建议阅读以下内容: https : //github.com/angular/angular.js/wiki/Understanding-Scopes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM