【问题标题】:"select form" does not sync with model“选择表格”与模型不同步
【发布时间】:2014-04-22 21:55:32
【问题描述】:

这是我的模板:

<div class="form-group" 
  ng-repeat="recipeIngredient in recipes.currentRecipe._ingredients">
  <label class="col-sm-2 control-label">Ingredient {{ $index + 1 }}</label>
  <div class="col-sm-10 form-inline" role="form">
    <input 
      type="text" 
      class="form-control" 
      placeholder="quantity" 
      ng-model="recipeIngredient.quantity"
    >
    <input 
      type="text" 
      class="form-control" 
      ng-model="recipeIngredient._unit"
    >
    <select 
      class="form-control" 
      ng-model="recipeIngredient._unit" 
      ng-options="unit._id for unit in units" 
    />
    <select 
      ng-model="recipeIngredient._ingredient" 
      class="form-control" 
      ng-model="recipeIngredient._ingredient" 
      ng-options="ingredient._id for ingredient in ingredients" 
    />
    <button 
      type="button" 
      class="btn btn-default btn-xs glyphicon glyphicon-remove" 
      ng-click="recipes.currentRecipe.removeRecipeIngredient($index)">
    </button>
  </div>
</div>

我想显示当前食谱中列出的成分 如果我使用:

<input type="text" class="form-control" ng-model="recipeIngredient._unit">

我可以看到 _unit 的值显示 但是为什么选择表单根本不显示recipeIngredient._unit的当前值?

 <select 
   class="form-control" 
   ng-model="recipeIngredient._unit" 
   ng-options="unit._id for unit in units" 
 />

我已验证 _unit 是单位列表的一部分 如果我将选择框的值更改为某个值,输入文本显示“对象”,这意味着模型的实际值是单位对象而不是unit._id文本

ng-options="unit._id for unit in units"`

如果您需要其他代码,请告诉我

【问题讨论】:

  • 如果你需要额外的代码,请告诉我 --> jsfiddle / plnkr 总是好的。
  • 我终于找到了解决问题的方法!

标签: angularjs


【解决方案1】:

我在使用时发现:

ng-options="unit._id for unit in units"

下拉框中显示的文本是unit._id,但ng-model 将是单位中的对象,而不是显示的文本! 为了纠正我的问题,我这样写:

<select ng-model="recipeIngredient._unit" class="form-control">
<option ng-repeat="unit in getId(units, '_id')">{{unit}}</option>
</select>

getId 是从units 返回_id 列表的函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2020-02-03
    相关资源
    最近更新 更多