【发布时间】:2016-03-02 00:40:29
【问题描述】:
我正在尝试向从 ng-model 接收它的变量添加一个值。
这里是 HTML:
<div ng-form="amountForm" class="form-group" id="inputField">
<input value="1" type="number" placeholder="1" ng-model="itemAmount" ng-maxlength="2" required>
</div>
<div ng-form="nameForm" class="form-group">
<input value="" type="text" placeholder="Name of Item" ng-model="itemName" ng-maxlength="10" required>
</div>
<select ng-model="currentDependent">
<option ng-repeat="item in items" value="{{item.dependentOn}}" ng-selected="currentDependent == item.dependentOn">{{item.name}}</option>
</select>
这是 Angular($scope.itemAmount 和 $scope.itemName 有效):
$scope.items = [];
$scope.addItem = function () {
console.log($scope.itemAmount + ", " + $scope.itemName + ", " + $scope.currentDependent);
$scope.items.push({
amount: $scope.itemAmount,
name: $scope.itemName,
showMe: false,
dependentOn: $scope.currentDependent
});
};
控制台上似乎没有打印任何内容:
所以唯一不起作用的是 Select 中的 ng-model。下拉列表显示正确的值。如何获取在下拉列表中选择的值以在控制台中打印?
注意:为了清楚起见,我省略了 http.post 代码。
【问题讨论】:
-
尝试使用字典。即 ng-model 的 item.itemname。
标签: angularjs data-binding angular-ngmodel angularjs-select