我正在尝试将值添加到从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代码。
您应该使用itemName(或其他item属性)来跟踪依赖关系。
<select ng-model="currentDependent">
<option ng-repeat="item in items" value="{{item.name}}">{{item.name}}</option>
</select>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句