AngularJS - ng ng repeat中的模型不会更改范围值

时间:2014-09-20 18:44:34

标签: javascript angularjs

我正在制作调查应用。用户可以选择“文本”,“段落”,“复选框”类型的问题,然后键入他们的问题。如果这是一个复选框,那么他们可以根据需要添加任意数量的选项。

以下代码不完整,仅显示“复选框”的大小写。这是指令的链接功能:

scope.questions = [];
scope.addQuestion = function(type)
{
    var question = {
        type : type,
        question : ''
    };
    if (type == 'checkbox') {
        question.options = [];
    }
    scope.questions.push(question);
}

scope.addOption = function($questionIndex)
{
    var question = scope.questions[$questionIndex],
        option   = 'Option ' + (question.options.length+1);

    question.options.push(option);
}

这是我的HTML

<div ng-repeat="question in questions" class="questions">
    <!-- The Question -->
    <div class="row">
        <!-- This is where the question field goes -->
    </div>

    <!-- The Answer -->
    <div class="row">

        <!-- Checkbox answer -->
        <div class="columns large-2" ng-if="question.type == 'checkbox'">Options</div>
        <div class="large-8 columns" ng-if="question.type == 'checkbox'">
            <div class="row" ng-repeat="option in question.options">
                <div class="large-offset-1 large-9 columns">
                    <input type="text" ng-model="option" />
                </div>
                <div class="large-1 columns end" ng-click="removeOption($parent.$index, $index)">
                    <i class="fa fa-times"></i>
                </div>
            </div>

            <!-- Button for adding more options MARKUP -->
            <div ng-click="addOption($parent.$index)"></div>
        </div>
    </div>
</div>

<!-- Add a new Question MARKUP -->
<!-- This calls addQuestion(type) with type being 'text', 'paragraph', 'checkbox', etc -->

问题是:当我添加更多选项并修改该值,然后添加以添加另一个选项时,值将重置为默认值。

目前,ng-model内的input是来自option的变量ng-repeat="option in question.options track by $index"。如果我将此ng-model更改为ng-model="question.options[$index]"那么一切正常。为什么option的{​​{1}}未通过引用传递?这是因为ng-repeat这里只是一个文本变量吗?如果是这样我怎么解决这个问题?

这是

plunker

0 个答案:

没有答案
相关问题