0

I'm trying to add a value to a variable receiving it from ng-model.

Here is the 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>

Here is the Angular ($scope.itemAmount and $scope.itemName works):

$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
    });
};

Nothing seems to be printing out on the console:

enter image description here

So the only thing is not working is ng-model in the Select. The drop down list is showing the correct values. How do I get the value selected in the drop down list to print in the console?

Note: I have left out the http.post code for clarity.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
nealous3
  • 712
  • 1
  • 11
  • 20

2 Answers2

1

You should use itemName (or other item property) to track dependency.

<select ng-model="currentDependent">
    <option ng-repeat="item in items" value="{{item.name}}">{{item.name}}</option>
</select>
SSH
  • 2,533
  • 16
  • 21
1

I had to use a dropdown menu in one of my projects recently and here is how I personally did it

http://jsfiddle.net/halirgb/Lvc0u55v/

Don't forget to open up your console to see the values!

At first, I thought its a good idea to just ng-repeat my options. But then I figured out using ng-options is more convenient when you get the hang of it.

This SO question explains it really well!

Community
  • 1
  • 1
realappie
  • 4,656
  • 2
  • 29
  • 38
  • 1
    Thanks, using ng-options is a better option for a drop down list. But it seems to be working well with ng-repeat. – nealous3 Mar 02 '16 at 02:31