使用ng-if的动态模型-AngularJS表单

854

这是我的代码:

如您所见,只有在q.answer == 'Fail'-这是页面上方的选择框的值时,此选项才会显示

如果选择了“失败”,则用户可以选择一种缺陷类型,将其保存到中question.failure.type

我的问题是,如果用户选择“失败”,然后选择一种缺陷类型-然后将“失败”更改为其他内容。然后question.failure.type即使ng-if现在为假,仍保留我如何才能做到这一点,question.failure.type如果theng-if为false 则不再存在于模型中

<div class="panel panel-danger" ng-if="q.answer == 'Fail'">
    <div class="panel-heading">
        <h3 class="panel-title">Comments</h3>
    </div>
    <div class="panel-body">
        <form novalidate name="failureForm">
            <div class="row">
                <div class="col-xs-12">
                    <select name='defect' class='form-control' ng-model='question.failure.type' ng-options='item for item in defectTypes' required>
                        <option value=''>Select a defect type...</option>
                    </select>

                    ...

编辑:根据要求添加新代码

<div ng-repeat="q in questions" ng-show="standard.serviceType">
    <div class="panel panel-info">
        <div class="panel-heading">
            <h3 class="panel-title">{{ $index + 1 }}. <span ng-bind="q.questionText"></span></h3>
        </div>
        <div class="panel-body">
            <answer-field question="q"></answer-field>
        </div>
    </div>
    ... then the code in the example above appears
克莱斯

假设在HTML元素中q.answer分配了的值,<input>可以question.failure.type使用来基于任何条件表达式清除的值ng-change

例如:

<input ng-bind='q.answer' 
       ng-change='question.failure.type =
                 (q.answer == 'Fail' ? question.failure.type : undefined)'
>

这只是一个简单的示例,但实际上是根据是否设置将设置为question.failure.type它已经具有的值或。undefinedq.answer == 'Fail'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章