【问题标题】:Javascript angularjs <select> has default value 0, when it should be 1Javascript angularjs <select> 有默认值 0,当它应该是 1
【发布时间】:2018-08-01 10:34:06
【问题描述】:

这是我的html代码:

      <table>
          <div ng-repeat="product in products" value="{{product}}">
             <div id="order_beacon" style="display: inline-flex; width: 100%;" class="row">
                 <span class="beacon_quantity">Quantity
                    <select id="beacon_count"  class="beacon_count"  ng-model="product.quantity">
                        <option ng-repeat="option in quantityOptions" value="{{option}}">{{option}}</option>
                    </select>
                 </span>
             </div>
          </div>
      </table>

在我的 JS 中,我从服务器获取产品列表,甚至硬编码所有数量为 1。

  for (var j = 0; j < $scope.productsAll.length; j++) {
    $scope.productsAll[j].quantity = 1;
}

如您所见,我的选择标签将 ng-model 设置为 product.quantity。那么不应该把默认值设置成那个吗?

【问题讨论】:

  • 您应该在填充下拉选项时使用ng-options 而不是ng-repeat。它更优雅地处理正确转换对象属性。
  • 你有没有初始化过product.quantity = 1?有一个Minimal, Complete, and Verifiable example 会有所帮助。
  • 是的,我做到了。感谢 Claies,我需要使用 ng-options。

标签: javascript html angularjs html-select angular-ngmodel


【解决方案1】:
<table>
          <div ng-repeat="product in products" value="{{product}}">
             <div id="order_beacon" style="display: inline-flex; width: 100%;" class="row">
                 <span class="beacon_quantity">Quantity
                    <select id="beacon_count"  class="beacon_count"  ng-model="product.quantity" ng-options="b as b for b in quantityOptions">

                    </select>
                 </span>
             </div>
          </div>
      </table>

【讨论】:

    猜你喜欢
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多