【发布时间】: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