javascript - Angular 选择无法正确更新模型

标签 javascript angularjs coffeescript

在我的 AngularJS 应用程序中,我有一个 select那是绑定(bind)到模型的。
当用户从列表中选择一个选项时,模型不会更新。
当用户再次单击相同的选项时,模型会更新。

如您所见,这不是理想的行为。在我的测试中,我替换了 select<input type='text'>它按预期工作。我的问题是什么?我无法在 JSFiddle 中复制它,所以我将发布我认为相关的尽可能多的代码。

索引.html

这是我为每种类型的可设置属性获得正确的模板。
所以 text-input 得到一个 input type='text'frequenty得到他的模板,一个 select .

<div ng-repeat="item in propertyList">
    <ng-include src="getTemplate(item, formObject)"></ng-include>
</div>

Controller .coffee

这里返回了正确的模板,并设置了可选择的频率。
$scope.getTemplate = (prop, object) =>  
   if $builder.widgets.hasOwnProperty prop #Here it found the frequency dropdown template.
     return $builder.widgets[prop]               
   else if angular.isString propValue
        return 'templates/widgets/textInput.html'
   else if angular.isObject propValue
        return 'templates/widgets/formulas.html'
   return

$scope.frequencies = [
    "NONE"
    "DOCUMENT"
    "PERIOD"
    "COLUMN"
  ]

频率.html

这是问题区域。选择显示选项,但单击选项时,模型不会更新。最奇怪的是 formula.html表已更新。这是因为它们绑定(bind)了同一个formObject,但是model没有更新,select被清空了。当我再次单击相同的选项时,select正确更新模型。

当您更换 select带有评论 input ,它完美无瑕。但是用户需要选择频率而不是输入频率。
<div>
    <!--<input ng-model="formObject[item]">-->
    <select data-ng-model="formObject[item]" data-ng-options="frequency for frequency in frequencies"></select>
</div>

公式.html
<div>
    <table class="table table-condensed">
        <thead>
        <tr>
            <th scope="row">&nbsp;</th>
            <th scope="col">title</th>
        </tr>
        </thead>
        <tbody>
            <tr ng-repeat="(key,row) in formObject.formulas">

                <th>{{key}}</th>
                <td ng-repeat="column in row track by $index">
                    <input type="text" ng-model="row[$index]"/>
                </td>
            </tr>
        </tbody>
    </table>
</div>

我希望这足够清楚,所以我希望你能帮助我解决这个问题。

最佳答案

您需要为 ng-model 使用一个点。
在此处查看我的解释和指向更详细信息的链接:
https://stackoverflow.com/a/30850255/3687474

关于javascript - Angular 选择无法正确更新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30869990/

相关文章:

coffeescript - 具有多个回调的冰 CoffeeScript

ruby-on-rails - 执行JS :Runtime Error somehow related to javascript/coffeescript

javascript - Backbone.Marionette 1.2没有方法 'bindTo'

javascript - 对 Saiku Analytics javascript 文件感到困惑

javascript - Await 是 Promise 中的保留字

javascript - document.createElements 抛出 InvalidCaracterError 异常

angularjs - ng-mouseover 和 ng-mouseleave 事件在 chrome 上不起作用

javascript - 使用 Promise 提交带有 submit() 的表单

javascript - 如何使用 Angular js 过滤数组

angularjs - 更新数组时如何避免刷新 ng-repeat DOM 列表